用hibernate向mysql 数据库中平添的数据是乱码
用hibernate向mysql 数据库中添加的数据是乱码
用hibernate封装的方法save()向mysql 数据库中添加的数据是乱码,安装数据库的时候用的是utf-8字符集,创建数据库和表的时候也是用的utf-8。但是用save方法插入记录的时候插入到数据库中是乱码,如果用insert语句插入汉字就没有问题
hibernate.cfg.xml文件:
解决方案:在与数据库连接的时候指定字符集,
用hibernate封装的方法save()向mysql 数据库中添加的数据是乱码,安装数据库的时候用的是utf-8字符集,创建数据库和表的时候也是用的utf-8。但是用save方法插入记录的时候插入到数据库中是乱码,如果用insert语句插入汉字就没有问题
hibernate.cfg.xml文件:
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///databases</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!--第一次创建表的时候应该用create --> <property name="show_sql">true</property> <mapping resource="com/test/hibernate/model/ClientUser.hbm.xml" /> </session-factory> </hibernate-configuration>
解决方案:在与数据库连接的时候指定字符集,
<property name="hibernate.connection.url">jdbc:mysql:///databases?useUnicode=true&characterEncoding=UTF-8</property>