日期:2014-05-16  浏览次数:20975 次

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



------解决方案--------------------
兄弟我也碰到你这个问题,不过我是用VS.NET做开发的,它是跟你的日期格式有关系的,我发现MYSQL日期格式只认"YYYY-MM-DD",而我们通常直接获取的日期格式是"MM-DD-YYYY",我是这样去处理的String.Format("{0:YYYY-mm-DD",DateTime.now()),希望对你有点用.
------解决方案--------------------
慢慢改吧
------解决方案--------------------
看你语句中update='2011-12-10 10:50:10' 这里,update是关键字,如果你表里有这个字段的话,是不是应该加上表名.update呢。
另外,时间可以用这样的方式赋值:
Calendar calendar = Calendar.getInstance(TimeZone .getTimeZone("GMT+8"));
Date date = calendar.getTime();
类.set你的时间字段(date);
再保存。