数据库字符集为latin1;
表test源代码USE `test`;CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
jsp页面<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.sql.*"%>
<% String result = ""; // 查询结果字符串
String sql = "select * from test"; // 连接字符串,格式: "jdbc:数据库驱动名称://数据库服务器ip/数据库名称?user=用户名&password=密码&使用Unicode=布尔值&字符编码=编码"
String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gbk";
String username = "test";
String password = "test"; Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(sql);
while ( rs.next() )
{
String aaa = rs.getString(2);
byte b[]=aaa.getBytes("latin1");
aaa=new String(b,"gbk");
result += "\n 第二个字段内容:" + aaa + "<BR>";
}
rs.close();
stmt.close();
conn.close();
%>
<HTML>
<BODY>
<%=result%>
</BODY>
</HTML>
这样读取表中繁体字时显示乱码,简体字正常,在mysql command line client 里面查询时显示正常。
不知道哪里有问题,谢谢大家。
表test源代码USE `test`;CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
jsp页面<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.sql.*"%>
<% String result = ""; // 查询结果字符串
String sql = "select * from test"; // 连接字符串,格式: "jdbc:数据库驱动名称://数据库服务器ip/数据库名称?user=用户名&password=密码&使用Unicode=布尔值&字符编码=编码"
String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gbk";
String username = "test";
String password = "test"; Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(sql);
while ( rs.next() )
{
String aaa = rs.getString(2);
byte b[]=aaa.getBytes("latin1");
aaa=new String(b,"gbk");
result += "\n 第二个字段内容:" + aaa + "<BR>";
}
rs.close();
stmt.close();
conn.close();
%>
<HTML>
<BODY>
<%=result%>
</BODY>
</HTML>
这样读取表中繁体字时显示乱码,简体字正常,在mysql command line client 里面查询时显示正常。
不知道哪里有问题,谢谢大家。
若上述方法不行,那么你直接在mysql控制台下,键入set names 'gbk';JSP页面中保持gbk编码再试试。