org.hibernate.exception.DataException: Could not execute JDBC batch update

ljh0242 2009-03-18 04:09:08
今天向mysql中插入数据出现很奇怪问题
User p1=new User();
p1.setName("测试数据");
p1.setPassword("12345");
System.out.println("insert suceess");
当p1.setName("")中不为中文,运行正常,当为中文就出错org.hibernate.exception.DataException: Could not execute JDBC batch update
说明p1.setName("")为mysql中user表对应字段的值
...全文
4491 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ihito 2012-02-28
  • 打赏
  • 举报
回复
3L正解
lmily15 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fei1502816 的回复:]
肯定是 数据库里边存的数据类型的长度太短了 试试存个长度为200的
[/Quote]
+1,
我遇到过相似的问题,就是因为数据库的栏位长度不够。其他情况就不清楚了
muler1988 2010-05-19
  • 打赏
  • 举报
回复
肯定是 数据库里边存的数据类型的长度太短了 试试存个长度为200的
ljh0242 2009-03-18
  • 打赏
  • 举报
回复
我的qq:178070373,欢迎加入java技术交流群48419050,交流java开发技术,促进技术进步!!!
ljh0242 2009-03-18
  • 打赏
  • 举报
回复
我用 自动产生表,并插入数据
hibernate.properties文件配置如下
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/usertest?useUnicode=true&characterEncoding=gbk
hibernate.connection.username root
hibernate.connection.password root
测试类文件如下:
package org.apple.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;



class UserTest{

private static SessionFactory sessions;

public static void main(String[] args) throws Exception{
//���û���������xmlӳ���ļ�
Configuration conf= new Configuration()
.addClass(User.class);

//��ɲ����sql���ļ�����ǰĿ¼������ݿ�
SchemaExport dbExport=new SchemaExport(conf);
dbExport.create(true, true);
sessions = conf.buildSessionFactory();
//���϶���Щ�̶���ʽ�Ļ�������

//start......
Session s = sessions.openSession();
Transaction t = s.beginTransaction();
System.out.println("start create table ");
//1.����ͨʹ�÷�ʽ��b����������
User p1=new User();
p1.setName("刘");
p1.setPassword("123456�㶫����");
System.out.println("insert suceess");

//2.持久化
s.save(p1);
//��ʱp1�Ѿ���������ݿ�

t.commit();
System.out.println("save suceess");
s.close();

}
}


奇怪的是p1.setName("刘");中为中文则报上面的错,不为中文,运行好好地

ljh0242 2009-03-18
  • 打赏
  • 举报
回复
drop table if exists User
17:02:27,406 WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
17:02:27,406 WARN JDBCExceptionReporter:49 - Unknown table 'user'
create table User (id varchar(255) not null, name varchar(255), password varchar(255), primary key (id))
17:02:27,593 WARN JDBCExceptionReporter:48 - SQL Warning: 1051, SQLState: 42S02
17:02:27,593 WARN JDBCExceptionReporter:49 - Unknown table 'user'
start create table
insert suceess
17:02:27,984 WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 22001
17:02:27,984 ERROR JDBCExceptionReporter:72 - Data truncation: Data too long for column 'name' at row 1
17:02:27,984 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.apple.hibernate.UserTest.main(UserTest.java:40)
Caused by: java.sql.BatchUpdateException: Data truncation: Data too long for column 'name' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:648)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 8 more
Exception in thread "main" org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.apple.hibernate.UserTest.main(UserTest.java:40)
Caused by: java.sql.BatchUpdateException: Data truncation: Data too long for column 'name' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:648)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 8 more
qingkongxiaoyang 2009-03-18
  • 打赏
  • 举报
回复
我之前遇到过Mysql数据库插入中文出错的问题,但是只是出现乱码,可以正常执行结束啊。
你可以从以下几方面查查看看。
1.你安装数据库时,是否更改字符集为GBK.
2.最好采用最新的驱动包,好像是5.1.6吧,具体的记不清了。
3..你的name属性长度设置足够长吗?是不是长度越界了?
你看一下详细的报错文件,应该能查出问题所在。
祝你好运啦。呵呵!
zhj92lxs 2009-03-18
  • 打赏
  • 举报
回复
你的数据库是什么字符集的,还有你的配置文件是什么样的

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧