回 帖 发 新 帖 刷新版面

主题:[讨论]jsp中无法正常显示数据库中的中文数据问题?请进!

<%@ page contentType="text/html;chaset=GBK" %>
<%@ page import="java.sql.*" %>

<%!
   Connection con=null;
%>

<html>
<body>
<%
  Statement stmt=null;
  ResultSet rs=null;
  try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  }catch(ClassNotFoundException e){}
  if(con==null){
     try{
        con=DriverManager.getConnection("jdbc:odbc:redsun","","");
        stmt=con.createStatement();
        rs=stmt.executeQuery("SELECT * FROM XS");
        out.print("NO.1");
     }catch(SQLException e1){
        out.print("未联接数据库");
     }
  }else{
     synchronized(con){
        try{
            con=DriverManager.getConnection("jdbc:odbc:redsun","","");
            stmt=con.createStatement();
            rs=stmt.executeQuery("SELECT * FROM XS");
            out.print("NO.2");
        }catch(SQLException e2){
            out.print("未联接数据库2");
        }
     }
  }
%>
<%
  try{
      out.print("<TABLE BORDER>");
      out.print("<TR>");
        out.print("<TH width=100>"+"学号");
        out.print("<TH width=100>"+"姓名");
        out.print("<TH width=100>"+"专业");
        out.print("<TH width=100>"+"性别");
        out.print("<TH width=100>"+"出生时间");
        out.print("<TH width=100>"+"总学分");
        out.print("<TH width=100>"+"备注");
      out.print("</TR>");
      while(rs.next()){
        out.print("<TR>");
          out.print("<TD width=100>"+rs.getString(1));
          out.print("<TD width=100>"+rs.getString(2));
          out.print("<TD width=100>"+rs.getString(3));
          out.print("<TD width=100>"+rs.getByte(4));
          out.print("<TD width=100>"+rs.getDate(5));
          out.print("<TD width=100>"+rs.getInt(6));
          out.print("<TD width=100>"+rs.getString(7));
        out.print("</TR>");
      }
      out.print("</TABLE>");
      con.close();
  }catch(SQLException e2){}
%>
</body>
</html>


页面中中文字符全都无法显示!!请高手指点!

回复列表 (共2个回复)

沙发

TOMCAT 5.0 
WIN XP
Eclipse3.1+lomboz+SQL_server

然后我又加工了一下,但这次却抛出NullPointException
下面给出修改的程序和异常报告:
<%@ page contentType="text/html;chaset=GBK" %>
<%@ page import="java.sql.*" %>

<%!
   Connection con=null;
%>

<html>
<body>
<%
  Statement stmt=null;
  ResultSet rs=null;
  try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  }catch(ClassNotFoundException e){}
  if(con==null){
     try{
        con=DriverManager.getConnection("jdbc:odbc:redsun","","");
        stmt=con.createStatement();
        rs=stmt.executeQuery("SELECT * FROM XS");
        out.print("NO.1");
     }catch(SQLException e1){
        out.print("未联接数据库");
     }
  }else{
     synchronized(con){
        try{
            con=DriverManager.getConnection("jdbc:odbc:redsun","","");
            stmt=con.createStatement();
            rs=stmt.executeQuery("SELECT * FROM XS");
            out.print("NO.2");
        }catch(SQLException e2){
            out.print("未联接数据库2");
        }
     }
  }
%>
<%
  try{
      out.print("<TABLE BORDER>");
      out.print("<TR>");
        String str=new String("学号");
       byte b[]=str.getBytes("ISO-8859-1");
        str=new String(b);
        out.print("<TH width=100>"+str);
        str=new String("姓名");
        b=str.getBytes("ISO-8859-1");
        str=new String(b);
        out.print("<TH width=100>"+str);
        str=new String("专业");
        b=str.getBytes("ISO-8859-1");
        str=new String(b);
        out.print("<TH width=100>"+str);
        str=new String("性别");
        b=str.getBytes("ISO-8859-1");
        str=new String(b);
        out.print("<TH width=100>"+str);
        str=new String("出生时间");
        b=str.getBytes("ISO-8859-1");
        str=new String(b);
        out.print("<TH width=100>"+str);
        str=new String("总学分");
        b=str.getBytes("ISO-8859-1");
        str=new String(b);
        out.print("<TH width=100>"+str);
        str=new String("备注");
        b=str.getBytes("ISO-8859-1");
        str=new String(b);
        out.print("<TH width=100>"+str);
      out.print("</TR>");
      while(rs.next()){
        out.print("<TR>");
          str=rs.getString(1);
          b=str.getBytes("ISO-8859-1");
          str=new String(b);
          out.print("<TH width=100>"+str);
          str=rs.getString(2);
          b=str.getBytes("ISO-8859-1");
          str=new String(b);
          out.print("<TH width=100>"+str);
          str=rs.getString(3);
          b=str.getBytes("ISO-8859-1");
          str=new String(b);
          out.print("<TH width=100>"+str);
          out.print("<TH width=100>"+rs.getByte(4));
          out.print("<TH width=100>"+rs.getDate(5));
          out.print("<TH width=100>"+rs.getInt(6));
          str=rs.getString(7);
          b=str.getBytes("ISO-8859-1");
          str=new String(b);
          out.print("<TH width=100>"+str);
        out.print("</TR>");
      }
      out.print("</TABLE>");
      con.close();
  }catch(SQLException e2){
      out.print("error!!");
  }
%>
</body>
</html>


type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause 

java.lang.NullPointerException
    org.apache.jsp.MyJsp_jsp._jspService(MyJsp_jsp.java:134)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

板凳

<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.sql.*" %>

<html>
<body>
<%
    Connection con;
    Statement stmt;
    ResultSet rs;
   try{
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   }catch(ClassNotFoundException e3){}
   try{
       con=DriverManager.getConnection("jdbc:odbc:redsun","","");
       stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
       rs=stmt.executeQuery("SELECT * FROM XS");
       int showPage=1;
%>

<%
       rs.absolute(showPage);
       out.print("<Table Border>");
      out.print("<TR>");
        out.print("<TH width=100>"+"学号");
        out.print("<TH width=100>"+"姓名");
        out.print("<TH width=100>"+"专业");
        out.print("<TH width=100>"+"性别");
        out.print("<TH width=100>"+"出生时间");
        out.print("<TH width=100>"+"总学分");
        out.print("<TH width=100>"+"备注");
      out.print("</TR>");
      while(!rs.isAfterLast()){
        out.print("<TR>");
          out.print("<TD width=100>"+rs.getString(1));
          out.print("<TD width=100>"+rs.getString(2));
          out.print("<TD width=100>"+rs.getString(3));
          out.print("<TD width=100>"+rs.getByte(4));
          out.print("<TD width=100>"+rs.getDate(5));
          out.print("<TD width=100>"+rs.getInt(6));
          out.print("<TD width=100>"+rs.getString(7));
        out.print("</TR>");
        rs.next();
      }
      out.print("</Table>");
      con.close();
  }catch(SQLException e2){
       out.print("未联接数据库");
  }
%>

</form>
</body>
</html>


这样试验可以显示中文.晕~~为什么会这样呢?

我来回复

您尚未登录,请登录后再回复。点此登录或注册