BUG描述:删除bbs帖子时能够删除成功,但是后台爆出SQLException。异常描述信息忘了,反正是比较常规的错误,但是在jsp内不好调试
解决办法:删一段运行一段,可惜运行时MyEclipse卡死,无奈强退。再打开时代码已经是删除后的结果了。虽然这次运行没错误,但是同样的,问题代码也不见了
最后又补上了代码,重新运行,也没有任何错误,功能上没问题。
教训:以后出现BUg时都要把问题代码备份一遍,再来debug。
附上改正后的jsp代码。
<%--
该jsp功能是把传入的楼层id删除并把回复它的楼层一并斩草除根
以及把被删除楼层回复的楼层叶子节点设为1
--%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!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">
<meta http-equiv="refresh" content="3;url=ShowArticleTree.jsp"/>
<title>删除记录</title>
</head>
<body>
<%!
private void delete(Connection conn,int id){//要删除的楼层号
Statement stmt=null;
ResultSet rs=null;
try{
stmt=conn.createStatement();
stmt.executeUpdate("delete from article where id="+id);//删除本楼层的数据库记录
rs=stmt.executeQuery("select * from article where pid="+id);
while(rs.next()){
delete(conn,rs.getInt("id"));
}
}catch(SQLException e){
//e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
finally{
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}
}
%>
<%
int id=Integer.parseInt(request.getParameter("id"));
int pid=Integer.parseInt(request.getParameter("pid"));//接受自被删除帖子的pid,即它的父亲
new Driver();
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=tiger");
conn.setAutoCommit(false);
delete(conn,id);//调用该函数递归删除回复及以下回复
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select count(*) from article where pid="+pid);//找父亲下的孩子们的总数
rs.next();
int count=rs.getInt(1);
if(count==0){
stmt.executeUpdate("update article set isleaf=0 where id="+pid);
}
conn.commit();
conn.setAutoCommit(true);
stmt.close();
conn.close();
%>
删除成功,3秒内自动跳转至主页面。
</body>
</html>