日期:2014-05-20  浏览次数:20745 次

mysql的date类存入问题
向大家请教一个问题,今天在写代码的时候碰到一个难题,我在数据库中有个字段类型设置为了Date类型。在java中能正常的取出来,并显示,但我在存入的时候遇到了难题。我首先看了看hibernate生成的映射,该字段的类型是Date,那我想,存入的时候我只要new一个Date(是util包下的)对象直接存入就行了吧,结果就过报错了。然后我想可能是长度的问题,因为从数据库中取出来的对象直接输出的话是精确到毫秒的,然后我就用simlpeDateFormat转换一个date对象精确到毫秒,存入数据库的时候还是出错了,同样的错误,当场奔溃。请大家指教一下啊。
Java code
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at MySessionClass.test4.main(test4.java:38)
Caused by: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update='2011-12-10 10:50:10' where Sid='40000001'' at line 1
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 8 more



------解决方案--------------------
'update='2011-12-10 10:50:10' where Sid='40000001''

你是要更新时间吗? update tablename set 时间字段='2011-12-10 10:50:10' where sid=xxxxx


------解决方案--------------------
update是关键字吧,你当作字段名要特殊处理,用ESC下面那个键按出来的东西包裹一下。在Hibernate的映射文件中column=`update`
------解决方案--------------------
数据库中的date日期和java。util。Date中的Date是不一样的啊
------解决方案--------------------
可能是你的*.hbm.xml里面对应字段的date类型和你javaBean中不一致,所以会出错,看看吧!