(哥们儿们啊能不能帮我举个例子啊,一个用树型结构展现BBS数据库数据的例子,包括数据库的定义和程序的实现!那样的话我感激不尽了)
我做的是出错了,刚开始虽然有些小问题但还能运行。但最后有不行了,直到我调试得再也没有办法了!具体如下:
最好能给我一个BBS数据库树形展现的简单例子,包括数据库定义,和算法的实现(跪下了)
定义的mysql数据库
<%--
Document : index
Created on : 2008-9-26, 11:29:31
Author : caobaoshusheng
--%>
<%@page contentType="text/html" pageEncoding="gbk"%>
<%@ page import ="java.lang.*"%>
<%@ page import = "java.sql.*"%>
<%!
String pstring = "";
private void tree(Connection conn, int id, int floor) {
Statement stat = null;
ResultSet rs = null;
String ssql = "";
String flstring = "";
int i;
for (i = 0; i < floor; i++) {
flstring = flstring + "-----";
}
try {
stat = conn.createStatement();
ssql = "select * from BbsRecord where sid=" + id;
while (rs.next()) {
rs = stat.executeQuery(ssql);
pstring = pstring + "<tr><td>" + rs.getInt("id") + "</td><td>" + flstring + rs.getString("title") + "</td><td>" +
"" + "</td><td>" + rs.getDate("time") + "</td></tr>";
if (rs.getInt("leaf") == 0) {
tree(conn, rs.getInt("id"), floor + 1);
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
rs.close();//提示是说这里有问题!
stat.close();
conn.close();
} catch (SQLException e) {
System.out.println("close erro!");
}
}
}
%>
<%
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://127.0.0.1:3306/test?user=root&password=159753";
Connection conn = DriverManager.getConnection(url);
tree(conn, 1, 0);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>BBS Mysql</title>
</head>
<body>
<center>
<table>
<%= pstring%>
<%pstring = "";%>
</table>
</center>
</body>
</html>
错误报告:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 36
33: System.out.println(e.getMessage());
34: } finally {
35: try {
36: rs.close();
37: stat.close();
38: conn.close();
39: } catch (SQLException e) {
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
org.apache.jsp.index_jsp.tree(index_jsp.java:39)
org.apache.jsp.index_jsp._jspService(index_jsp.java:100)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
Apache Tomcat/6.0.16