首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

通过jsp表单post的中文字符在sqlserver表中varchar,nvarchar列中存储为&35775;&26085;&2130

2012-03-03 
通过jsp表单post的中文字符在sqlserver表中varchar,nvarchar列中存储为在数据库表中汉字存储为每个汉字占8

通过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.getParameter( "nchar1 ")%>
        <%=insert_sql%>
    </body>
</html>

----------submit3.jsp-------------


<html>
<head>
<title> JSP的中文处理 </title>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
</head>

<body>
<form   name= "form1 "   method= "post "   action= "sql_test3.jsp ">
<div   align= "center ">
<input   type= "text "   name= "char1 ">
<input   type= "text "   name= "nchar1 ">
<input   type= "submit "   name= "Submit "   value= "Submit ">
</div>
</form>
</body>
</html>


[解决办法]
不会是把地址存进去了吧
[解决办法]
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.*,java.util.Date,java.text.DateFormat " pageEncoding= "GBK " errorPage= " " %>
加个 pageEncoding= "GBK " 试试
[解决办法]
去掉url的useUnicode=true;characterEncoding=GBK;试试

热点排行