删除子父节点中的某个节点
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class deleteNoteMysql {
public static void main(String args[]) throws SQLException {
Connection conn = DB.getConn();
Statement stmt = conn.createStatement();
ResultSet rs = null;
int id = 33;
rs = stmt.executeQuery(“select * from category where id=33”);
rs.next();
int a = rs.getInt(7);
int pidd = rs.getInt(2);
if (pidd == 0) {
de(id, a);
} else {
de(id, a);
rs = stmt.executeQuery(“select count(*) from category where pid =”
+ pidd);
System.out.println(“pidd=” + pidd);
rs.next();
int count = rs.getInt(1);
if (count <= 0) {
stmt.executeUpdate(“update category set isLeaf=0 where id= “
+ pidd);
}
}
System.out.println(a);
}
private static void de(int id, int isleaf) throws SQLException {//如果删除的节点的父节点没有其他节点需要设置父节点的idLeaf为0
// TODO Auto-generated method stub
Connection conn = DB.getConn();
Statement st = conn.createStatement();
if (isleaf == 0) {
st.execute(“delete from category where id =” + id);
} else {
ResultSet rs = st
.executeQuery(“select * from category where pid =” + id);
while (rs.next()) {
System.out.println(rs.getInt(1));
de(rs.getInt(1), rs.getInt(7));
}
st.execute(“delete from category where id =” + id);
}
}
}
/*
import java.sql.*;
public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(“jdbc//localhost/shopping?user=root&password=admin”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
*/
/*
Navicat MySQL Data Transfer
Source Server : root
Source Server Version : 50527
Source Host : localhost:3306
Source Database : shopping
Target Server Type : MYSQL
Target Server Version : 50527
File Encoding : 65001
Date: 2013-02-23 10:53:44
SET FOREIGN_KEY_CHECKS=0;
-- ——————————————
-- Table structure for `category`
-- ——————————————
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pid` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`descr` varchar(255) DEFAULT NULL,
`cno` int(11) DEFAULT NULL,
`grade` int(11) DEFAULT NULL,
`isLeaf` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8;
-- ——————————————
-- Records of category
-- ——————————————
INSERT INTO `category` VALUES (‘31’, ‘0’, ‘a’, ‘a’, ‘2147483647’, ‘1’, ‘1’);
INSERT INTO `category` VALUES (‘32’, ‘0’, ‘b’, ‘b’, ‘-2’, ‘1’, ‘1’);
INSERT INTO `category` VALUES (‘33’, ‘32’, ‘aa’, ‘aa’, ‘2147483645’, ‘2’, ‘1’);
INSERT INTO `category` VALUES (‘34’, ‘33’, ‘aaa’, ‘fahdgjhdfjkgjkl’, ‘-4’, ‘3’, ‘1’);
INSERT INTO `category` VALUES (‘35’, ‘34’, ‘aaaa’, ‘aaaa’, ‘2147483643’, ‘4’, ‘0’);
INSERT INTO `category` VALUES (‘36’, ‘31’, ‘afgas’, ‘asg’, ‘-2’, ‘2’, ‘1’);
INSERT INTO `category` VALUES (‘37’, ‘36’, ‘asg’, ‘asfhsdh’, ‘2147483645’, ‘3’, ‘0’);
INSERT INTO `category` VALUES (‘38’, ‘36’, ‘’, ‘’, ‘-4’, ‘3’, ‘0’);
INSERT INTO `category` VALUES (‘39’, ‘31’, ‘dh’, ‘fdgjhfd’, ‘2147483645’, ‘2’, ‘0’);
INSERT INTO `category` VALUES (‘40’, ‘31’, ‘dfgh’, ‘dfjhdfj’, ‘-4’, ‘2’, ‘0’);
*/
还没有评论,来说两句吧...