初学JSP 。 现在做了一个模拟程序,初步模拟论坛。主要有如下的代码:<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=*******";
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='ShowArticleDetail.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();%>
pid是父节点id。
isleaf是判断是不是叶子节点。
其中tree()是一个递归函数, 把子帖全部找出来也累加在str里。输出是这样的结果:1 蚂蚁大战大象
2 ----大象被打趴下了
3 --------蚂蚁也不好过
4 --------瞎说
5 ------------没有瞎说
9 --------大象进医院了
10 ------------护士是蚂蚁
6 ----怎么可能
7 --------怎么没有可能
8 --------可能性是很大的 输出代码:
<table>
<%= str %>
</table>但是现在每次刷新网页,上面的内容就会多出来一遍, 刷N遍就多出来N遍。。这个是跟着教程写的,可是教程里没有这样的问题。。
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=*******";
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='ShowArticleDetail.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();%>
pid是父节点id。
isleaf是判断是不是叶子节点。
其中tree()是一个递归函数, 把子帖全部找出来也累加在str里。输出是这样的结果:1 蚂蚁大战大象
2 ----大象被打趴下了
3 --------蚂蚁也不好过
4 --------瞎说
5 ------------没有瞎说
9 --------大象进医院了
10 ------------护士是蚂蚁
6 ----怎么可能
7 --------怎么没有可能
8 --------可能性是很大的 输出代码:
<table>
<%= str %>
</table>但是现在每次刷新网页,上面的内容就会多出来一遍, 刷N遍就多出来N遍。。这个是跟着教程写的,可是教程里没有这样的问题。。
解决方案 »
- 透明图标显示
- tomcat6.0 config文件只读属性为什么改不了
- jsp网页自动关闭
- 项目中碰到的applet servlet传递数据的问题 希望各位帮帮忙 很急
- 急急急问!!在线等package javax.servlet.jsp找不到
- 求救:jsp使用javaBean的路径问题?
- struts2.0返回到ajax的值被截断了。
- 用水晶报表,JFreeReport等生成HTML后,如何分页打印
- 求jboss 幫助文檔,有中文最好
- j2ee的配置问题,我用的是win98
- hibernate+spring 编写的CRUD制作
- javabean如何实现指定日期加5后,得出5天后的日期呢,即日期加数值返回日期的javabean代码
private void tree(Connection conn, int id , int level){
Statement stmt = null;
ResultSet rs = null;
String strPre = "";
for(int i = 0 ; i< level; i++){
strPre += "----";
}
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>" +
strPre + "<a href = 'ShowArticleDetail.jsp?id=" + rs.getInt("id") + "'>" + rs.getString("title") +"</a>"
+ "</td></tr>";
if(rs.getInt("isleaf")!=0){
tree(conn , rs.getInt("id"), level+1);
}
}
}catch省略。
比如servelet那段,还有他最后讲structs spring 那些的。