用SQL读oracle的问题,求解
private void del(Connection conn, int id)
{
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = conn.createStatement();
String sql = "select * from \ "bbs\ ".\ "article\ " where pid = " + id;
rs = stmt.executeQuery(sql);
while(rs.next())
{
int sid = rs.getInt( "id ");
System.out.println( "找到其子结点 " + sid);
del(conn, sid);
}
stmt.executeUpdate( "delete from \ "bbs\ ".\ "article\ " where id = " + id);
System.out.println( "删除此结点 " + id);
Statement disIdSt = conn.createStatement();
ResultSet disIdRs = disIdSt.executeQuery
( "select id from \ "bbs\ ".\ "article\ " where id > " + id );
while(disIdRs.next())
{
int disId = disIdRs.getInt(1);
System.out.println( "找到需要更改的结点 " + disId);
disIdSt.executeUpdate
( "update \ "bbs\ ".\ "article\ " set id = " + (disId - 1)
+ "where id = " + disId );
System.out.println( "修改id " + disId + "为 " + (disId - 1));
disIdSt.executeUpdate
( "update \ "bbs\ ".\ "article\ " set pid = " + (disId - 1)
+ "where pid = " + disId );
System.out.println( "修改pid " + disId + "为 " + (disId - 1));
}
disIdSt.close();
disIdRs.close();
这是del方法的一部分,删除一个树形结构的结点
测试树形结构如下:
id(结点号) pid(根结点号) 结构图
11 10 11
12 11 ---- 12
13 12 ---- 13
14 13 ---- 14
15 11 ---- 15