package com.syc.category;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import com.syc.java.DB;
public class CategoryDAO {
public static void save(Category c){
Connection conn=null;
PreparedStatement pstate=null;
try {
conn=DB.getConnection();
String sql="insert into category value (null,?,?,?,?,?)";
pstate=DB.getPreparedStatement(conn, sql);
pstate.setString(1, c.getName());
pstate.setString(2, c.getDescription());
pstate.setInt(3, c.getPid());
pstate.setInt(4, c.isLeaf()?0:1);
pstate.setInt(5, c.getGrade());
pstate.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeStatement(pstate);
DB.closeConnection(conn);
}
}
public static void getCategories(List<Category> categories,int id){
Connection conn=null;
try {
conn=DB.getConnection();
getCategories(conn,categories,id);
}finally{
DB.closeConnection(conn);
}
}
public static void getCategories(Connection conn,List<Category> categories,int id){
Statement state=null;
ResultSet reSet=null;
try {
String sql="select * from category where pid="+id;
state=DB.getStatement(conn);
reSet=DB.executeQuery(state, sql);
while(reSet.next()){
Category c=new Category();
c.setId(reSet.getInt("id"));
c.setDescription(reSet.getString("description"));
c.setGrade(reSet.getInt("grade"));
c.setLeaf(reSet.getInt("isleaf")==0?true:false);
c.setPid(reSet.getInt("pid"));
c.setName(reSet.getString("name"));
categories.add(c);
if(!c.isLeaf()){
getCategories(conn,categories,reSet.getInt("id"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeResultSet(reSet);
DB.closeStatement(state);
}
}
public static void savechild(int pid,String name,String description){
Connection conn=null;
Statement state=null;
PreparedStatement pstate=null;
ResultSet reSet=null;
try {
conn=DB.getConnection();
conn.setAutoCommit(false);
state=DB.getStatement(conn);
reSet=DB.executeQuery(state, "select * from category where id="+pid);
reSet.next();
String sql="insert into category value (null,?,?,?,?,?)";
pstate=DB.getPreparedStatement(conn, sql);
pstate.setString(1, name);
pstate.setString(2, description);
pstate.setInt(3, pid);
pstate.setInt(4, 0);
pstate.setInt(5, reSet.getInt("grade")+1);
state.executeUpdate("update category set isleaf=1 where id="+pid);
pstate.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
DB.closeResultSet(reSet);
DB.closeStatement(state);
DB.closeStatement(pstate);
DB.closeConnection(conn);
}
}
public static void deleteCategory(int id, int pid) {
Connection conn=null;
Statement state=null;
ResultSet reSet=null;
try {
conn=DB.getConnection();
conn.setAutoCommit(false);
state=DB.getStatement(conn);
delete(conn,id);
reSet=DB.executeQuery(state, "select count(*) from category where pid="+pid);
reSet.next();
int count=reSet.getInt(1);
if(count<=0){
state.executeUpdate("update category set isleaf=0 where id="+pid);
}
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
DB.closeResultSet(reSet);
DB.closeStatement(state);
DB.closeConnection(conn);
}
}
public static void delete(Connection conn,int id) {
Statement state1=null;
Statement state2=null;
ResultSet reSet=null;
try {
conn=DB.getConnection();
state1=DB.getStatement(conn);
state2=DB.getStatement(conn);
reSet=DB.executeQuery(state2, "select * from category where pid="+id);
state1.executeUpdate("delete from category where id="+id);
while(reSet.next()){
delete(conn, reSet.getInt("id"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.closeResultSet(reSet);
DB.closeStatement(state1);
DB.closeStatement(state2);
}
}
}
最近下载更多
komorebi123987 LV5
2023年12月9日
qiuaizhang LV3
2023年4月10日
copy1000 LV1
2022年9月30日
00000007 LV1
2022年7月15日
284650 LV1
2022年6月12日
illkih LV2
2022年3月17日
3361758107 LV1
2021年12月19日
201901150233 LV1
2021年12月12日
A Lazy dog LV1
2021年12月11日
mwk545924364 LV4
2021年12月9日
最近浏览更多
微信网友_7562058687860736
6月20日
暂无贡献等级
wwy000 LV2
4月29日
asashx
2024年12月26日
暂无贡献等级
Mhgfhjyrf LV3
2024年12月9日
我做梦 LV2
2024年12月3日
吴明艳
2024年11月25日
暂无贡献等级
微信网友_7060373407682560 LV1
2024年7月1日
15719908287 LV10
2024年6月19日
kuyedie LV1
2024年6月17日
xcccffff LV1
2024年6月11日

