Hibernate插入mysql数据库中文乱码解决
使用ant工具编译程序,运行程序.命令:ant -f xxx.xml指定生成文件;ant run编译并运行文件。
如果Hibernate将中文插入mysql数据库,调用select语句中文显示乱码,则mysql的编码方式设为gbk,注意这里是小写,不用重新安装mysql数据库,将MySQL Server 5.1目录下的my.ini配置文件里的所有default-character-set设为gbk,注意小写,即可。
同时自己创建hibernate数据库时要设定数据库的默认编码方式:
create database hibernate default charset=gbk;
这样仍不行,考虑设定hibernate数据库中表的默认编码方式。(很重要)另外,在java源代码中n.setTitle("你好吗");之前也可添加转码语句,根据情况而定,这里可有可无。
String na="";
try{
na="疯狂联盟成立了";
byte bb[]=na.getBytes("GBK");
na=new String(bb);
}catch(UnsupportedEncodingException ex){
throw new RuntimeException("unsupported encoding type.");
}
然后在hibernate.cfg.xml属性文件里添加
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
中文乱码问题解决,一切OK。
这里UTF-8也可为GB2312,GBK。
注意要添加import java.io.*;
相关文档:
【书名】高性能MySQL(第二版)
【原书名】High Performance MySQL, second edition
【作者】Baron Schwartz,Peter Zaitsev,Vadim
Tkachenko,Jeremy D.Zawodny,Arjen Lentz,Derek
J.Balling 著
【译者】王小东 李军 康建勋 译
【出版社】电子工业出版社
【书号】978- ......
创建数据库:
mysql>create database test;
Query OK, 1 row affected (0.02 sec)
创建数据库表:
mysql>use test;
Database changed
创建数据库表:
myaql>create table user(SID VARCHAR(11), name VARCHAT(6) );
Query OK, 0 rows affected (0.08 sec)
插入记录:
mysql>insert int use ......
//得分计算四舍五入
SELECT ROUND((SUM(getfeng)/SUM(totalfeng))*100) as feng from answerdata WHERE uid='151' AND targetid IS NOT NULL
1.ceil () /ceiling() 向上取整
例: ceil(1.2) = 2
2.floor () 向下取整
例: floor(1.2) = 1
3.round() 四舍五入
&n ......
public class select {
public List XiuGai_select(String keyword){
List list=new ArrayList();
Connection conn = null;
Statement stmt = null;
String sql=null;
ResultSet res = null;
get ......
对mysql的优化不在行,搞过几次优化,但是都不是很理想,还是浪费资源太多。一直发现我的mysql的缓存命中率极差,情况良好的时候到达过60-70%,但是运行时间一长,只有10-20%。查了一些资料,关于缓存的一些参数记录
mysql> SHOW VARIABLES LIKE ‘%query_cache%’;
+—————&m ......