在写程序的时候需要调用数据库中的数据,要经过查询调用的。可是很蛋痛的是,使用带有中文的查询语句时根本查不出来。我还一直以为是我前面写的有问题呢,查了半天才在昨晚发现了问题的关键。
我用的查询语句是。
select * from studentinfo where class ='一班' and major='计算机系'
如果把中文改成英文,同样的,数据库里面的数据也改掉,这样是可以查到的。
网上搜了半天,才知道是字符集的原因,其实我也想到了,只不过不知道到底怎么改。改好的主要代码是。
View Code JAVA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public Connection getConnection() { Connection con = null; String url1 ="jdbc:mysql://localhost/schoolmanage"; String username = "root"; String password = "123456"; try{ Class.forName("com.mysql.jdbc.Driver"); //con = DriverManager.getConnection(url1,username,password);//这里是我以前的代码 con = DriverManager.getConnection("jdbc:mysql://localhost/schoolmanage?user=root&password=123456&useUnicode=true&characterEncoding=GBK" );//这句是修改后的 }catch(Exception e){ e.printStackTrace(); } return con; } |
希望对你有用!
>> 若为原创,转载请注明: 转载自Laycher's Blog
>> 本文链接地址: java使用MySQL不支持中文查询
>> 订阅本站: http://feed.feedsky.com/laycher
我统一用的utf-8~支持中文