📄 categoryaction.java
字号:
package com.test.ps.category;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import com.test.ps.util.DBUtil;
public class CategoryAction {
public static final CategoryAction category=new CategoryAction();
private String sql;
public static int n;
private CategoryAction(){
}
//增加类别
public void addCategory(GoodsCategory cate){
try{
if(cate.getParentId()!=0){
sql="insert into goodscategory(parentId,categoryName) values('"
+cate.getParentId()+"','"+cate.getCategoryName()+"')";
}
else{
sql="insert into goodscategory(parentId,categoryName) values('0','"+cate.getCategoryName()+"')";
}
DBUtil.executeUpdate(sql);
}catch(Exception ex){
ex.printStackTrace();
}
}
//修改类别
public void changeCategory(int oCateId,GoodsCategory newCate){
try{
sql="update goodscategory " +
"set parentId="+ newCate.getParentId()+ ",categoryName='"+newCate.getCategoryName()+
"'"+"where categoryId="+oCateId;
DBUtil.executeUpdate(sql);
}catch(Exception ex){
ex.printStackTrace();
}
}
//删除类别
public void deleteCategory(int cateId,String type){
if(type.equals("par")){
try{
sql="delete from goods where categoryId=(select categoryId from goodscategory where parentId="+cateId+")";
DBUtil.executeUpdate(sql);
sql="delete from goodscategory where parentId="+cateId;
DBUtil.executeUpdate(sql);
sql="delete from goodscategory where categoryId=" +cateId;
DBUtil.executeUpdate(sql);
}catch(Exception ex){
ex.printStackTrace();
}
}
else{
try{
sql="delete from goodscategory where categoryId=" +cateId;
DBUtil.executeUpdate(sql);
sql="delete from goods where categoryId="+cateId;
DBUtil.executeUpdate(sql);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public static ArrayList<GoodsCategory> getGoodsCategory()
{ //System.out.println("parentok1");
ArrayList<GoodsCategory> gcList=new ArrayList<GoodsCategory>();
String sql="select * from GoodsCategory where parentId=0";
// System.out.println("ok2");
ResultSet rs=DBUtil.getResult(sql);
try {
rs.last();
n=rs.getRow();
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs.first();
do
{
//System.out.println("parentok4");
GoodsCategory goodsCategory=new GoodsCategory();
goodsCategory.setCategoryId(rs.getInt("categoryId"));
goodsCategory.setCategoryName(rs.getString("categoryName"));
goodsCategory.setParentId(rs.getInt("parentId"));
//System.out.println("parentok5");
gcList.add(goodsCategory);
//System.out.println("parentok6");
}while(rs.next());
} catch (SQLException e) {
e.printStackTrace();
}
return gcList;
}
//获取相应子类别
public static ArrayList<GoodsCategory> getSonGoodsCategory(int parentId)
{
ArrayList<GoodsCategory> gcSonList=new ArrayList<GoodsCategory>();
String sql="select * from GoodsCategory where parentId='"+ parentId+"'";
ResultSet rs=DBUtil.getResult(sql);
try {
//+++++
if(rs.first()){
rs.first();
do{
GoodsCategory goodsCategory=new GoodsCategory();
goodsCategory.setCategoryId(rs.getInt("categoryId"));
goodsCategory.setCategoryName(rs.getString("categoryName"));
goodsCategory.setParentId(rs.getInt("parentId"));
gcSonList.add(goodsCategory);
}while(rs.next());
}
} catch (SQLException e) {
e.printStackTrace();
}
return gcSonList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -