往mysql数据库存入中文数据存到数据库中的是乱码,
从MySql数据库读出中文数据,读出来的全是乱码,这是为什么?
怎么解决?
确认MySql数据库安装的时候选中的是GB2312,用的开发工具是Lomboz Eclipse
不知道有那位大侠能帮帮忙!!!
谢谢!代码如下:import java.sql.*;
public class Testchinese
{
public static void main(String[] args)
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//String url="jdbc:mysql://localhost:3306/bbs";
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/BBS","root","123456");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from article");
while(rs.next())
{
String str=rs.getBytes("cont").toString();//用这个输出得到的是ASCII码,怎么转换成中文
// System.out.println(rs.getString("cont")); 用这个输出得到是乱码
}
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//我在另一个文件中直接用insert语句插入中文的,结果插到数据库中的是乱码?各位帮帮忙,感激不尽!
从MySql数据库读出中文数据,读出来的全是乱码,这是为什么?
怎么解决?
确认MySql数据库安装的时候选中的是GB2312,用的开发工具是Lomboz Eclipse
不知道有那位大侠能帮帮忙!!!
谢谢!代码如下:import java.sql.*;
public class Testchinese
{
public static void main(String[] args)
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//String url="jdbc:mysql://localhost:3306/bbs";
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/BBS","root","123456");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from article");
while(rs.next())
{
String str=rs.getBytes("cont").toString();//用这个输出得到的是ASCII码,怎么转换成中文
// System.out.println(rs.getString("cont")); 用这个输出得到是乱码
}
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//我在另一个文件中直接用insert语句插入中文的,结果插到数据库中的是乱码?各位帮帮忙,感激不尽!
然后在直接查询一下,看看显示是否正确,
如果正确,尝试以下方法
new String(rs.getBytes("cont"),"GBK");
new String(rs.getBytes("cont"),"GB2312");
new String(rs.getBytes("cont"),"iso8859-1");
...
再次查询,注意:不要查询通过JDBC插入的数据,因为插入的时候可能已经存在编码问题
只查询 通过 mysql命令行客户端直接插入的数据
尽量多是几种编码格式 应该可以判断出来
就是编码转换的问题,插入数据的时候先把数据转换成 ISO-8859-1 再插入进去
数据读出来的时候,先把数据以ISO-8859-1读出
再转换成gb2312就可以正常显示了
再次感谢两位回答!