关于request.getParameter的问题

danxing1981 2008-01-20 03:00:29
我在上一个页面中通过超级链接向下一个页面传了一个参数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>
...全文
650 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
rigger21 2008-01-20
  • 打赏
  • 举报
回复
你先看一下在页面中是不是穿过去值了。然后在使用request获取
老紫竹 2008-01-20
  • 打赏
  • 举报
回复
Class.forName("com.mysql.jdbc.Driver"); 
String url="jdbc:mysql://localhost/bbs?user=root&password=root&useUnicode=true&characterEncoding=GBK"; // 增加一些参数看看
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();


少写了一个&
老紫竹 2008-01-20
  • 打赏
  • 举报
回复
Class.forName("com.mysql.jdbc.Driver"); 
String url="jdbc:mysql://localhost/bbs?user=root&password=rootamp;useUnicode=true&characterEncoding=GBK"; // 增加一些参数看看
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧