日期:2014-05-18  浏览次数:20708 次

通过jsp表单post的中文字符在sqlserver表中varchar,nvarchar列中存储为访日区
在数据库表中汉字存储为访日区每个汉字占8个字节,看着像是unicode编码,但web页面的输入输出中文显示均正常。更奇怪的是,通过查询分析器,插入的记录汉字却完全正常,不会是存储为这类字符。

我尝试换过sqlserver的jdbc驱动,试过filter,在jsp文件前加入 <%@   page   contentType= "text/html;   charset=gb2312 "   ...,还有request.setCharacterEncoding( "GBK ");也试过getBytes的转换等方法,均没有任何效果。困惑我好久了,请各位高手答疑。

以下是我的测试代码:
---------------sql_test3.jsp------------
<%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.*,java.util.Date,java.text.DateFormat "   errorPage= " "   %>

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN "   "http://www.w3.org/TR/html4/loose.dtd ">
<html>
    <head>
        <meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
        <title> 无标题文档 </title>
    </head>
    <%
request.setCharacterEncoding( "GBK ");

        Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();  
        String   url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;useUnicode=true;characterEncoding=GBK; ";  
        String   user= "sa ";  
        String   password= "123 ";  
        int   result=0;
        Connection   conn=   DriverManager.getConnection(url,user,password);  
        Statement   stmt=conn.createStatement         (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  

        String   insert_sql= "insert   into   test(no,char1,nchar1)   values( ";  
        insert_sql=insert_sql+ "3 "+ ", ";
        insert_sql=insert_sql+ " ' "+request.getParameter( "char1 ")+ " ' "+ ", ";
        insert_sql=insert_sql+ " ' "+request.getParameter( "nchar1 ")+ " ' "+ ") ";
//         insert_sql=insert_sql+ " ' "+new   String(request.getParameter( "char1 ").getBytes( "ISO_8859_1 "),   "gb2312 ")+ " ' "+ ", ";
//         insert_sql=insert_sql+ " ' "+new   String(request.getParameter( "nchar1 ").getBytes( "ISO_8859_1 "),   "gb2312 ")+ " ' "+ ") ";

//         out.println(insert_sql);    

        result=stmt.executeUpdate(insert_sql);  

        stmt.close();
        conn.close();
//response.sendRedirect( "submit.jsp ");
    %>
    <body>
        <%=request.getParameter( "char1 ")%>
        <%=request.getP