使用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。