我是hibernate的新手,我正在开发一个使用MySQl,Hibernate和Struts的应用程序。对于给定的代码,我总是得到以下错误。我确实谷歌这个错误,但那些线程没有帮助。
错误
javax.servlet.ServletException: Could not execute JDBC batch update
at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
....
代码
Session session = new HibernateSession().currentSession(this.getServlet().getServletContext());
if(auctionform.getId() == null || auctionform.getId().intValue() == 0){
auction.setId(null);
auctionform.setId(null);
}else{
auction.setId(auctionform.getId());
Criteria criteria = session.createCriteria(Auction.class);
criteria.add(Restrictions.idEq(Integer.valueOf(req.getParameter("id"))));
auction = (Auction) criteria.uniqueResult();
}
Boolean isAuctionActive = false;
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date auctionEndingDate = DateUtils.getDate(req, "auctionenddate");
Date todaysDate = formatter.parse(formatter.format(currentDate.getTime()));
if(auctionEndingDate.after(todaysDate)){
isAuctionActive = true;
}
auction.setDescription(auctionform.getDescription());
auction.setDate(auctionform.getDate());
auction.setCurrentprice(auctionform.getCurrentprice());
....some code
Attachment attachment1 = AttachmentUtils.saveAttachment(auctionform, session, auction, 0);
Attachment attachment2 = AttachmentUtils.saveAttachment(auctionform, session, auction, 1);
Attachment attachment3 = AttachmentUtils.saveAttachment(auctionform, session, auction, 2);
Transaction transaction = session.beginTransaction();
if(attachment1!=null){
session.save(attachment1.getDbFile());
session.save(attachment1.getThmnailDbFile());
session.save(attachment1);
auction.setFirstimage(attachment1);
}
if(attachment2!=null){
session.save(attachment2.getDbFile());
session.save(attachment2.getThmnailDbFile());
session.save(attachment2);
auction.setSecoundimage(attachment2);
}
if(attachment3 != null){
session.save(attachment3.getDbFile());
session.save(attachment3.getThmnailDbFile());
session.save(attachment3);
auction.setSignimage(attachment3);
}
session.saveOrUpdate(auction);
transaction.commit();
session.flush();
我总是在transaction.commit()
行收到此错误。我没有得到出错的地方。
更新
我设置log4j并完成堆栈跟踪如下。
完成堆栈跟踪
Caused by: java.sql.BatchUpdateException: Incorrect string value: '\xE5\x8F\x96\xE3\x82\x8A...' for column 'BIDDERRESTRICTION' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 34 more
2012-10-25 17:26:29 [action] [ERROR] Servlet.service() for servlet action threw exception
java.sql.BatchUpdateException: Incorrect string value: '\xE5\x8F\x96\xE3\x82\x8A...' for column 'BIDDERRESTRICTION' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at net.startbahn.web.app.PostAuctionVPAction.execute(PostAuctionVPAction.java:175)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at net.startbahn.filters.PrivilegeManageFilter.doFilter(PrivilegeManageFilter.java:112)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at net.startbahn.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:121)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:722)
我正在尝试在列中存储一些日语内容,因此我尝试将表格整理更新为 COLLATE = ujis_japanese_ci 和 UTF-8 ,但仍然没效果。
将表及其所有字段的排序规则设置为 utf8-utf8_bin 对我来说非常有用。
答案 0 :(得分:0)
在jdbc连接中设置UTF8字符编码。这可以在hibernate.cfg.xml文件中完成,例如
<property name="connection.url">
jdbc:mysql://db-server:3306/databasename?useUnicode=true&characterEncoding=utf8
</property>
(也许只有两个选择中的一个是必要的。我没试过,我不使用日语;。)