这个问题怎么解决先谢谢大家了
时间:2008-08-29 18:01:25
来源:论坛整理 作者: 编辑:chinaitzhe
<%@ 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 = 'showdetail.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 = 'showdetile.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>
~~~~~~~~~~~~~~~~~~~~~~~~~~~
我的问题是:现在我每在浏览器里刷新一次页面,它就会重新读取一次数据库,会接着上一次已经读取出来的10条记录后面重复显示,也就变成了20条记录,再刷新一次,又重复显示10条,我用得是TOMCAT和lomboz eclipse,请问这个问题怎么解决?谢谢大家了!
网友回复:可以在显示数据之前先清理掉已有的数据。
网友回复:private void tree(Connection conn, int id, int level) {
str = ""; // 这里先清空。
网友回复:强烈建议
- Java code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ private String tree(Connection conn, int id, int level) { String str = ""; return str; }
否则你的程序有并发访问的漏洞!
网友回复:!String str =
这样定义了全局变量。。
改为String str =
就好了。。
网友回复:private void tree(Connection conn, int id, int level) {
str = ""; // 这里先清空。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这里清空?
那最后输出的只是最后一条记录了吧?
我是把所有的帖子信息全都输入到str里,最后打印输出啊!
网友回复: <%!String str = ""; %>
这样定义
str是全局变量,缓存了原先的数据。
改为
<% String str = ""; %>
网友回复:已改为
<% String str = ""; %>
但是无法被private void tree(Connection conn, int id, int level) 方法中的str引用
网友回复:
- Java code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ Statement stmt = conn.createStatement(); ResultSet rs = stmt .executeQuery("Select * from article where pid = 0"); str="";//在这里清空 while (rs.next()) { str = " <tr> <td>" rs.getInt("id") " </td> <td>" " <a href = 'showdetile.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(); %>
关键字:问题,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面