我在上一个页面中通过超级链接向下一个页面传了一个参数id,id的值可以从数据库中取出
在第二个页面中通过采用的方式进行输出,为什么报错呢?我用的是mysql数据库,从数据库中取出的汉字是乱码,和这有关吗?
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import ="java.sql.*"%>
<%!
String str = "";
private void tree(Connection conn, int id , int level) {
Statement stmt = null;
ResultSet rs = null;
String preStr ="";
for(int i=0;i<level; i++) {
preStr += "----";
}
try {
stmt = conn.createStatement();
String sql = "select * from article where pid=" + id;
rs = stmt.executeQuery(sql);
while (rs.next()) {
str += "<tr><td>" + rs.getInt("id") + "</td><td>" +
preStr + "<a href='ShowArticleDetail1.jsp?id= " + rs.getInt("id") + "'>" +
rs.getString("title") + "</a>" +
"</td></tr>";
if(rs.getInt("isleaf") !=0) {
tree(conn, rs.getInt("id"),level + 1);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs !=null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e ) {
e.printStackTrace();
}
}
}
%><%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article where pid = 0");
while (rs.next()) {
str += "<tr><td>" + rs.getInt("id") + "</td><td>" +
      "<a href ='ShowArticleDetail1.jsp?id =" + rs.getInt("id")+ "'>" + 
      rs.getString("title") +"</a>" + 
      "</td></tr>";
     if(rs.getInt("isleaf") !=0) {
     tree(conn, rs.getInt("id"), 1);
     }}
rs.close();
stmt.close();
conn.close();
%>
<!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=gbk">
<title>Insert title here</title>
</head><body>
<table border ="1">
<%= str %>
</table>
</body>
</html><%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<%
String strId = request.getParameter("id");
int id = Integer.parseInt(strId);Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article where id = " + id);%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head><body><%
if (rs.next()) {
%>
<table border="1">
<tr>
<td>ID</td>
<td><%= rs.getInt("id") %></td>
</tr> <tr>
<td>Title</td>
<td><%= rs.getString("title") %></td>
</tr> <tr>
<td>Content</td>
<td><%= rs.getString("cont") %></td>
</tr>
</table><%
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

解决方案 »

  1.   

    Class.forName("com.mysql.jdbc.Driver"); 
    String   url="jdbc:mysql://localhost/bbs?user=root&password=rootamp;useUnicode=true&amp;characterEncoding=GBK"; // 增加一些参数看看
    Connection   conn   =   DriverManager.getConnection(url); 
    Statement   stmt   =   conn.createStatement(); 
      

  2.   

    Class.forName("com.mysql.jdbc.Driver"); 
    String url="jdbc:mysql://localhost/bbs?user=root&password=root&amp;useUnicode=true&amp;characterEncoding=GBK"; // 增加一些参数看看
    Connection   conn   =   DriverManager.getConnection(url); 
    Statement   stmt   =   conn.createStatement(); 少写了一个&
      

  3.   

    你先看一下在页面中是不是穿过去值了。然后在使用request获取