来帮小弟看看这个JAVA程序
这段代码我是从书上摘抄下来的(不完全)。麻烦解释下是什么意思<%@pagelanguage="java"contentType="text/html;charset=gb...
这段代码我是从书上 摘抄下来的(不完全)。
麻烦解释下是什么意思
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<%!
private void del(Connection conn, int id){
Statement stmt = null;
ResultSet rs = null;
try{
stmt = conn.createStatement();
String sql = "select * from article where pid=" + id;
rs = stmt.executeQuery(sql);
while(rs.next()){
del(conn, rs.getInt("id"));
}
stmt.executeUpdate("delete from article where id = " + id);
}catch (SQLException e){
e.printStackTrace();
}finally {
try {
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
%>
<%
int id = Integer.parseInt(request.getParameter("id"));
int pid = Integer.parseInt(request.getParameter("pid"));
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=rott";
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(false);
del(conn, id);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from article where pid = " + pid);
rs.next();
int count = rs.getInt(1);
rs.close();
stmt.close();
if(count <= 0) {
Statement stmtUpdate = conn.createStatement();
stmtUpdate.executeUpdate("update article set isleaf = 0 where id = " + pid);
stmtUpdate.close();
}
conn.commit();
conn.setAutoCommit(true);
conn.close();
response.sendRedirect("ShowArticleTree.jsp");
%>
<!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>
</body>
</html>
特别是这段
del(conn, id);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from article where pid = " + pid);
rs.next();
int count = rs.getInt(1);
rs.close();
stmt.close();
if(count <= 0) {
Statement stmtUpdate = conn.createStatement();
stmtUpdate.executeUpdate("update article set isleaf = 0 where id = " + pid);
stmtUpdate.close();
}
谢谢
就是那个 rs.next()不太懂 展开
麻烦解释下是什么意思
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<%!
private void del(Connection conn, int id){
Statement stmt = null;
ResultSet rs = null;
try{
stmt = conn.createStatement();
String sql = "select * from article where pid=" + id;
rs = stmt.executeQuery(sql);
while(rs.next()){
del(conn, rs.getInt("id"));
}
stmt.executeUpdate("delete from article where id = " + id);
}catch (SQLException e){
e.printStackTrace();
}finally {
try {
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
%>
<%
int id = Integer.parseInt(request.getParameter("id"));
int pid = Integer.parseInt(request.getParameter("pid"));
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=rott";
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(false);
del(conn, id);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from article where pid = " + pid);
rs.next();
int count = rs.getInt(1);
rs.close();
stmt.close();
if(count <= 0) {
Statement stmtUpdate = conn.createStatement();
stmtUpdate.executeUpdate("update article set isleaf = 0 where id = " + pid);
stmtUpdate.close();
}
conn.commit();
conn.setAutoCommit(true);
conn.close();
response.sendRedirect("ShowArticleTree.jsp");
%>
<!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>
</body>
</html>
特别是这段
del(conn, id);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from article where pid = " + pid);
rs.next();
int count = rs.getInt(1);
rs.close();
stmt.close();
if(count <= 0) {
Statement stmtUpdate = conn.createStatement();
stmtUpdate.executeUpdate("update article set isleaf = 0 where id = " + pid);
stmtUpdate.close();
}
谢谢
就是那个 rs.next()不太懂 展开
3个回答
展开全部
。。。
rs.next()不懂的话 你还看jsp 建议你先看看集合遍历rs.next()是指向结果集的指针向下移动并且返回是否有纪录
rs.next()不懂的话 你还看jsp 建议你先看看集合遍历rs.next()是指向结果集的指针向下移动并且返回是否有纪录
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
del方法是用来删除id为pid的一条记录,rs.next()的意思是当查询出的ResultSet中有内容
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询