初学jsp,什么都不懂,我写了一个简单的处理数据库表的jsp程序,
可是从数据库中显示到页面上的中文变成了乱码,看了别人的解决方法好像也不行,各位高手帮我看看是怎么回事啊!!<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%request.setCharacterEncoding("GB2312");%>
<%//@page pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sdb";
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from depts";
//ResultSet rs=sdb.executeQuery(sql);
ResultSet rs=stmt.executeQuery(sql);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!-- TemplateBeginEditable name="doctitle" -->
<title>无标题文档</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head><body>
<%String id = "";
String name = "";
while(rs.next()) {
id = new String(rs.getString(1).getBytes("ISO-8859-1"),"gb2312");
name = new String(rs.getString(2).getBytes("ISO-8859-1"),"gb2312");
//id=rs.getString(1);
//name=rs.getString(2);
%>
<p>您的第一个字段内容为:<%=id%>
您的第二个字段内容为:<%=name%></p>
<p>
  <%}%>
  <%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

解决方案 »

  1.   

    lz下面这行是什么?
    <%//@page pageEncoding="GB2312"%>--------------------------------
    <%@ page language="java" pageEncoding="GB2312"%>
      

  2.   

    id = new String(rs.getString(1).getBytes("ISO-8859-1"),"gb2312");
    name = new String(rs.getString(2).getBytes("ISO-8859-1"),"gb2312");
    这两句话为什么要转换编码?数据库中是ISO-8859-1编码?如果数据库中是这个编码的话应该不会有中文显示吧,我们开发中从数据库取数据,不转换编码。只有在从上一页面取得中文数据的时候才转换。因为tomcat默认是ISO-8859-1编码,所以它在传递数据的时候会将编码转换成ISO-8859-1。这种直接从数据库取数据应该没有转换的必要。
      

  3.   

    看一下你的数据库中表字段的类型,具体我记不清了,但是我记得java开发要用可变长度的类型,即普通类型前面+n
      

  4.   

    你现在这个表的字段类型是什么?
    char改成nchar
    varchar改成nvarchar
      

  5.   

    页面加上:<%@ page pageEncoding="GBK"%>
      

  6.   


    加这个<%@  page  pageEncoding=  "GBK  "%  > 没用的。
    我自己解决了,
    id  =  new  String(rs.getString(1).getBytes("ISO-8859-1"),"gb2312");  
    name  =  new  String(rs.getString(2).getBytes("ISO-8859-1"),"gb2312");  
    改成
    id = new String(rs.getString(1).getBytes("GBK"));
    name = new String(rs.getString(2).getBytes("GBK"));
    就好了