eclipse3.2下解决数据插入mysql数据库时乱码无法插入问题(Hibernate)
2009-02-28 23:22
816 查看
/**
*
*/
package hibernate.ch06;
import hibernate.HibernateSessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* @author Administrator
*
*/
public class InsertData {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateSessionFactory.currentSession();
Transaction tx = null;
Student s = new Student();
s.setSno(1);
s.setSage(19);
s.setSname("Student");
s.setSdept("数学");
s.setSaddress("湖南省");
s.setSsex("男");
try{
tx = session.beginTransaction();
Integer id = (Integer)session.save(s);
System.out.println("id:" + id);
tx.commit();
}catch(Exception se){
tx.rollback();
se.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
}
}
如果就这样插入,在eclipse3.2中会报错:
2009-2-28 23:16:16 org.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 1366, SQLState: HY000
2009-2-28 23:16:16 org.hibernate.util.JDBCExceptionReporter logExceptions
严重: Incorrect string value: '/xCA/xFD/xD1/xA7' for column 'Sdept' at row 1
org.hibernate.exception.GenericJDBCException: could not insert: [hibernate.ch06.Student]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:559)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:543)
at hibernate.ch06.InsertData.main(InsertData.java:33)
Caused by: java.sql.SQLException: Incorrect string value: '/xCA/xFD/xD1/xA7' for column 'Sdept' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2901)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1584)
at com.mysql.jdbc.Connection.serverExecute(Connection.java:4742)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1251)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:993)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1930)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1855)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1693)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1968)
... 15 more
为什么会出现这样的情况呢,原来在hibernate.cfg.xml文件中, 已经设置:
<property name="connection.characterEncoding">gb2312</property>
而在eclipse中,所有字符都是默认为utf-8,所以在插入时mysql会报异常字符错误。有一个比较简便的方法,可以方便程序员在测试时可以随时修改,就是在做测试时把上面的属性改为utf-8。当在Web情况下时,jsp页面可以是GBK或GB2312,在布署时再把上面的属性改为gb2312或gbk.
<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--配置数据库驱动-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--配置数据库网络连接的url-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysql</property>
<!--配置数据库网络连接的用户名,默认一般为root-->
<property name="hibernate.connection.username">root</property>
<!--配置数据库网络连接的密码-->
<property name="hibernate.connection.password">mysql</property>
<!--配置数据库网络连接池的大小-->
<property name="hibernate.connection.pool.size">20</property>
<!--后台是否显示sql,开发时为true,运行时为false-->
<property name="hibernate.show_sql">true</property>
<!-- 设置JDBC的隔离级别-->
<property name="hibernate.connection.isolation">2</property>
<property name="hibernate.format_sql">true</property>
<!-- <property name="jdbc.fetch_size">50</property> -->
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">true</property>
<property name="connection.useUnicode">false</property>
<!--编码方式,最好是gbk,用gb2312有的字符不全-->
<property name="connection.characterEncoding">gb2312</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<mapping resource="hibernate/ch01/UserInfo.hbm.xml" />
<mapping resource="hibernate/ch06/Student.hbm.xml" />
<mapping resource="hibernate/ch06/Course.hbm.xml" />
<mapping resource="hibernate/ch06/SC.hbm.xml" />
</session-factory>
</hibernate-configuration>
*
*/
package hibernate.ch06;
import hibernate.HibernateSessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* @author Administrator
*
*/
public class InsertData {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateSessionFactory.currentSession();
Transaction tx = null;
Student s = new Student();
s.setSno(1);
s.setSage(19);
s.setSname("Student");
s.setSdept("数学");
s.setSaddress("湖南省");
s.setSsex("男");
try{
tx = session.beginTransaction();
Integer id = (Integer)session.save(s);
System.out.println("id:" + id);
tx.commit();
}catch(Exception se){
tx.rollback();
se.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
}
}
如果就这样插入,在eclipse3.2中会报错:
2009-2-28 23:16:16 org.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 1366, SQLState: HY000
2009-2-28 23:16:16 org.hibernate.util.JDBCExceptionReporter logExceptions
严重: Incorrect string value: '/xCA/xFD/xD1/xA7' for column 'Sdept' at row 1
org.hibernate.exception.GenericJDBCException: could not insert: [hibernate.ch06.Student]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:559)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:543)
at hibernate.ch06.InsertData.main(InsertData.java:33)
Caused by: java.sql.SQLException: Incorrect string value: '/xCA/xFD/xD1/xA7' for column 'Sdept' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2901)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1584)
at com.mysql.jdbc.Connection.serverExecute(Connection.java:4742)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1251)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:993)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1930)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1855)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1693)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1968)
... 15 more
为什么会出现这样的情况呢,原来在hibernate.cfg.xml文件中, 已经设置:
<property name="connection.characterEncoding">gb2312</property>
而在eclipse中,所有字符都是默认为utf-8,所以在插入时mysql会报异常字符错误。有一个比较简便的方法,可以方便程序员在测试时可以随时修改,就是在做测试时把上面的属性改为utf-8。当在Web情况下时,jsp页面可以是GBK或GB2312,在布署时再把上面的属性改为gb2312或gbk.
<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--配置数据库驱动-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--配置数据库网络连接的url-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysql</property>
<!--配置数据库网络连接的用户名,默认一般为root-->
<property name="hibernate.connection.username">root</property>
<!--配置数据库网络连接的密码-->
<property name="hibernate.connection.password">mysql</property>
<!--配置数据库网络连接池的大小-->
<property name="hibernate.connection.pool.size">20</property>
<!--后台是否显示sql,开发时为true,运行时为false-->
<property name="hibernate.show_sql">true</property>
<!-- 设置JDBC的隔离级别-->
<property name="hibernate.connection.isolation">2</property>
<property name="hibernate.format_sql">true</property>
<!-- <property name="jdbc.fetch_size">50</property> -->
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">true</property>
<property name="connection.useUnicode">false</property>
<!--编码方式,最好是gbk,用gb2312有的字符不全-->
<property name="connection.characterEncoding">gb2312</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<mapping resource="hibernate/ch01/UserInfo.hbm.xml" />
<mapping resource="hibernate/ch06/Student.hbm.xml" />
<mapping resource="hibernate/ch06/Course.hbm.xml" />
<mapping resource="hibernate/ch06/SC.hbm.xml" />
</session-factory>
</hibernate-configuration>
相关文章推荐
- eclipse插入数据到MySQL数据库时,出现中文乱码问题的解决方法
- 关于hibernate插入数据到mysql数据库中文乱码问题的解决
- 数据插入到MySQL数据库乱码的问题解决
- MySQL数据库 查询表数据出现中文乱码 及 插入中文报错Incorrect string value问题解决方法
- 用hibernate向mysql插入数据时问号乱码的问题解决!!
- hibernate插入数据到mysql数据库中乱码问题
- java web mysql数据库插入数据乱码问题解决方法
- 解决hibernate插入中文数据乱码的问题
- 解决mysql无法插入中文数据及插入后显示乱码的问题【亲测图文解说!】
- Mysql数据库插入数据时的乱码问题解决办法
- MySQL数据库插入数据,中文乱码问题解决
- 解决Hibernate向MySQL数据库插入中文乱码问题
- 解决jsp插入mysql数据库中的数据出现乱码问题
- 解决mysql无法插入中文数据及插入后显示乱码的问题【亲测图文解说!】【转载】
- eclipse链接mysql数据库,插入,查找出现乱码问题全方位解决。
- eclipse插入数据到mysql中的乱码问题解决
- 解决Hibernate向MySQL数据库插入中文乱码问题
- 解决mysql无法插入中文数据及插入后显示乱码的问题
- 解决php向mysql数据库插入数据乱码问题
- 解决mysql数据库中无法插入中文数据的问题