注册 登录
编程论坛 J2EE论坛

mysql查询出现乱码问题

cos100 发布于 2007-09-16 13:58, 1503 次点击

package org.jdbc.util;
import java.sql.*;
public class UseDatabase {
public static void main(String[] args) {
//String url="jdbc:mysql://localhost:3306/studentinfo?useUnicode=true&characterEncoding=gbk";
String url="jdbc:mysql://localhost:3306/studentinfo";
String userName="root";
String password="cos100";
String sql=null;
Connection con=null;
Statement stam=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url, userName, password);
stam=con.createStatement();
sql="select bname from bookinfo";//bookinfo为一个关于书籍信息的表  bname为书名
ResultSet re=stam.executeQuery(sql);
while(re.next()){
System.out.println(re.getString("bname"));//这里打印出来全是乱码...
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try{
stam.close();
con.close();
}catch(SQLException e){
System.out.println("close error");
}
}
}

}
表中的记录我是直接用读文件的形式加入的..(load data infile "bookinfo.txt" into table bookinfo)
怎么解决乱码问题哦。 
试了很多种方法也没解决..

7 回复
#2
无缘今生2007-09-16 14:02
请问你数据库里面的数据是怎么添加的?
如果是直接插入的话,那在读取的数据是应该能够正常显示的。
如果你是通过其他方式插入数据库的话,那你就要确定现在你的数据库里面存储的是不是就是乱码。
#3
cos1002007-09-16 14:59
我是 用 mysql>load data infile "bookinfo.txt" into table bookinfo
这条语句插入的啊。 现在的也是中文啊。
#4
luoxian_20032007-09-16 15:22
我以前做毕设的时候也是出现 ??? 这样的乱码,不知道你的是不是?
mysql的编码要改一下,改为GBK就行了
默认的编码好像不支持中文
#5
cos1002007-09-16 15:26
怎么改啊。 能不能具体点哦。 .谢了.
#6
luoxian_20032007-09-16 16:30

安装目录\bin
下有个MySQLInstanceConfig.exe文件,点击运行在里面改,5.0以上版本才有

或者在安装目录下找到my.ini文件


default-character-set=gbk


这一行,改一下

#7
cos1002007-09-16 22:07

我的是4.1..版本的啊。
default-character-set=gbk
今天我设置成default-character-set=utf-8
搞的我的出现了1067的错误啊。 搞了一下午,刚刚卸了所有的东西才装上,

#8
evollock2007-09-16 22:36
//String url="jdbc:mysql://localhost:3306/studentinfo?useUnicode=true&characterEncoding=gbk";
这句话你注释掉干嘛?
下面那句没注释的不符合规范吧
1