SQL Server的timestamp映射到Java的java.sql.Timestamp出现的问题

org.teasoft.bee.osql.BeeSQLException: 不支持从 timestamp 到 TIMESTAMP 的转换。
    at org.teasoft.honey.osql.core.ExceptionHelper.convert(ExceptionHelper.java:25)
    at org.teasoft.honey.osql.core.SqlLib.selectSomeField(SqlLib.java:194)
    at org.teasoft.honey.osql.core.SqlLib.select(SqlLib.java:69)
    at org.teasoft.honey.osql.core.ObjSQL.select(ObjSQL.java:85)
    at org.teasoft.honey.osql.core.ObjSQLRich.selectOne(ObjSQLRich.java:230)
    at org.teasoft.exam.bee.osql.UpdateSetExam.test(UpdateSetExam.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.teasoft.exam.comm.TestHelper.runTest(TestHelper.java:32)
    at org.teasoft.exam.bee.test.TestNormal.runTest(TestNormal.java:183)
    at org.teasoft.exam.bee.test.TestNormal.test(TestNormal.java:121)
    at org.teasoft.exam.MainDbTest.test(MainDbTest.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 不支持从 timestamp 到 TIMESTAMP 的转换。
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
    at com.microsoft.sqlserver.jdbc.DataTypes.throwConversionError(DataTypes.java:946)
    at com.microsoft.sqlserver.jdbc.ServerDTVImpl.getValue(dtv.java:1952)
    at com.microsoft.sqlserver.jdbc.DTV.getValue(dtv.java:175)
    at com.microsoft.sqlserver.jdbc.Column.getValue(Column.java:113)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:1982)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:1967)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getTimestamp(SQLServerResultSet.java:2368)
    at org.teasoft.honey.osql.core.HoneyUtil.getResultObjectByIndex(HoneyUtil.java:1275)
    at org.teasoft.honey.osql.core.SqlLib._getObjectByindex(SqlLib.java:1423)
    at org.teasoft.honey.osql.core.SqlLib.selectSomeField(SqlLib.java:164)
    
    
    SQL Server“timestamp”数据类型映射到java“byte”数据类型。

 obj = rs.getObject(i + 1); //Sql Server  :  (byte[]) [0, 0, 0, 0, 0, 0, 7, -41]

在SQL Server里,对应java.sql.Timestamp的是:

datetime,smalldatetime,datetime2

使用Bee的Ddl.java可以自动生成建表语句,省掉麻烦.

public class LeafAlloc implements Serializable {

	private Long id;
	private String bizTag;
	private Long maxId;
	private Integer step;
	private String description;
	private Timestamp updateTime;
	private Integer version;

//... get,set
}

就一句话, 即可根据javabean结构生成表.

Ddl.createTable(new LeafAlloc(), true);

生成表后打印的日志:

CREATE TABLE leaf_alloc (
id  bigint PRIMARY KEY NOT NULL,  
biz_tag  nvarchar(255) DEFAULT NULL,  
max_id  int DEFAULT NULL,  
step  int DEFAULT NULL,  
description  nvarchar(255) DEFAULT NULL,  
update_time  datetime DEFAULT NULL,  
version  int DEFAULT NULL  
 )

Bee,互联网新时代的Java ORM框架,更快、更简单、更自动,开发速度快,运行快,更智能!

https://github.com/automvc/bee

<think>嗯,用户遇到了一个Java中将LONG类型转换为java.sql.Timestamp问题,我需要帮他们解决。首先,我得看看错误信息是什么。用户提到的是“Unsupported conversion from LONG to java.sql.Timestamp”,这通常发生在数据库驱动或ORM框架(比如MyBatis)尝试将数据库中的LONG类型转换成JavaTimestamp时,但转换不支持。 首先,我应该考虑时间戳的存储方式。在Java中,时间戳通常用long类型表示毫秒数,而数据库中的时间戳字段如TIMESTAMP或DATETIME。如果数据库字段是LONG类型,但实际存储的是时间戳的毫秒数,这时候直接映射可能会出错,因为驱动可能不知道如何转换。 解决方案可能有几种。第一,检查数据库字段类型是否正确。如果存的是时间戳,应该用TIMESTAMP而不是LONG,这样驱动会自动处理转换。如果必须用LONG,就需要在Java代码中手动转换,比如用ResultSet的getLong方法取出long值,然后用new Timestamp(long)来创建Timestamp对象。 然后,用户可能使用了MyBatis,这时候需要检查实体类是否有正确的类型处理器。如果实体类的字段是Timestamp类型,但数据库是LONG,可能需要自定义TypeHandler,在映射时进行转换。同时,还要确保实体类有无参构造方法,否则MyBatis无法实例化对象,导致映射失败,这可能也是错误的原因之一,引用[4]提到了这一点。 另外,检查数据库驱动版本也很重要。旧版本的驱动可能不支持某些类型的转换,更新到最新版本可能解决问题。例如,引用[2]提到mssql-jdbcJava8时间类的转换异常,可能类似的问题。 如果问题出在JDBC查询,直接使用ResultSet.getLong()获取值,再转换为Timestamp。对于JPA或Hibernate,可能需要使用@Convert注解或AttributeConverter来自定义转换逻辑。 总结步骤应该是:确认数据库字段类型,检查驱动版本,手动转换long到Timestamp,检查实体类构造方法,必要时自定义类型处理器。同时,引用中的错误提示也提示了可能的构造方法问题,需要特别注意。</think>### 解决方案:LONG类型转换为java.sql.Timestamp #### 1. 问题根源分析 当数据库字段类型为`LONG`(如Oracle的NUMBER类型或DB2的BIGINT),而Java实体类期望接收`java.sql.Timestamp`时,会出现类型转换错误。这是因为: - 数据库驱动无法直接将数值类型(`LONG`)隐式转换为时间戳类型[^1][^3] - ORM框架(如MyBatis)在映射时缺少有效的类型转换器[^4] #### 2. 具体解决步骤 **步骤1:验证数据库字段类型** - 确认数据库中存储的是**时间戳数值**(如Unix时间戳毫秒值),而非日期格式 - 示例验证SQL: ```sql SELECT column_name, DATA_TYPE FROM all_tab_columns WHERE table_name = 'YOUR_TABLE'; ``` **步骤2:Java代码手动转换** ```java // 从ResultSet直接获取LONG类型 long epochMillis = resultSet.getLong("your_column"); Timestamp timestamp = new Timestamp(epochMillis); // 逆向转换(存储到数据库) preparedStatement.setLong(parameterIndex, timestamp.getTime()); ``` **步骤3:MyBatis类型处理器** ```xml <!-- 自定义TypeHandler --> public class LongToTimestampHandler implements TypeHandler<Timestamp> { @Override public void setParameter(PreparedStatement ps, int i, Timestamp parameter, JdbcType jdbcType) { ps.setLong(i, parameter.getTime()); } // 其他方法实现... } <!-- MyBatis配置 --> <resultMap> <result column="your_column" property="timestampField" typeHandler="com.example.LongToTimestampHandler"/> </resultMap> ``` **步骤4:检查实体类构造方法** - 确保实体类包含无参构造方法[^4] ```java public class YourEntity { public YourEntity() {} // 必须存在 // 其他字段和构造方法... } ``` #### 3. 兼容性检查 - 检查JDBC驱动版本(如Oracle需使用ojdbc8+,SQL Server使用mssql-jdbc 8.4+)[^2] - 对于DB2数据库,确认SQL错误代码格式(如`SQLSTATE 22007`表示日期格式错误)[^5] #### 4. 替代方案 ```sql -- 在数据库层转换(Oracle示例) SELECT TO_TIMESTAMP('1970-01-01', 'YYYY-MM-DD') + (your_column/86400000) FROM DUAL ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值