日期:2014-05-17  浏览次数:20771 次

jsp页面连接SqlServer2005数据库,查询后出现如下乱码情况 是为啥??
1 ?? ??  
2 ?? ??  
3 ?? ??  



数字能显示,可是汉字却不显示,代码如下
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'test.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  <%
  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
  String url="jdbc:sqlserver://localhost:1433;DatabaseName=test";
  String user="sa";
  String pwd="523325";
  Connection conn=DriverManager.getConnection(url,user,pwd);
  Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  String sql="select * from jilu";
  ResultSet rs=stmt.executeQuery(sql);
  while(rs.next()){
  %>
  <body>
  <table>
  <tr>
  <td><%=rs.getInt("id")%></td><td><%=rs.getString("name") %></td><td><%=rs.getString("sex") %></td>
  </tr>
  <%} %>
  <%rs.close();
  stmt.close();
  conn.close(); 
  %>
  </table>
  </body>
</html>


------解决方案--------------------
探讨

引用:

把pageEncoding="GB2312" 改为pageEncoding="UTF-8"试试
就是这个原因,已经解决了。我想问为什么要改成UTF-8,用什么样的编码方式跟什么因素有关,跟数据库有关吗?谢谢