⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wptype.java

📁 web版的进销存系统
💻 JAVA
字号:
package jxc.web;

import jxc.com.DBConnect;
import java.lang.String;
import jxc.util.StrFun;
import java.sql.*;

import javax.servlet.http.HttpServletRequest;


public class Wptype{
	private int ID;
	private int parentid;
	private String wptypename;
	private String path;
	private String depth;
//	----------------转换String为integer-------------------
	public int  getID() {
		return ID;
	}
	public void setID(int anew) {
		this.ID=anew;
	}
	public void setID(String anew){
		if (anew!=null)
			this.ID = Integer.parseInt(anew);
	} 
//	-----------------------------------------------------
	public String getDepth() {
		return depth;
	}
	public void setDepth(String depth) {
		this.depth = depth;
	}
	public int getParentid() {
		return parentid;
	}
	public void setParentid(int parentid) {
		this.parentid = parentid;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getWptypename() {
		return wptypename;
	}
	public void setWptypename(String wptypename) {
		this.wptypename = wptypename;
	}

	public boolean excute() throws Exception {
		String Str="Select * From wptype where ID="+ID;
		try {
			DBConnect dbc  = new DBConnect(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			ResultSet rs = dbc.executeQuery(Str);
			if (rs.next()){
				ID=rs.getInt("ID");
				wptypename=rs.getString("wptypename");	
			}
			rs.close();
			dbc.close();
			return true;
		}
		catch (SQLException sqle){
			return false;
		}		
	}

	public void Add(HttpServletRequest request) throws Exception {

		wptypename=StrFun.getString(request,"wptypename");  
		parentid=StrFun.getInt(request, "parentid");
		DBConnect dbc1  = new DBConnect();

		try {				
			String sqlinsert = "insert into wptype(wptypename,parentid) values('"
				+ wptypename + "','" + parentid + "')";
			dbc1.executeUpdate(sqlinsert);
		} catch (Exception e) {
			e.printStackTrace();
		}			

		ResultSet rs1 = null;
		try {
			//列出类中所有的类别
			String sqlSelect1 = "select * from wptype order by ID desc";
			rs1 = dbc1.executeQuery(sqlSelect1);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(rs1.next()){
			int id1 = rs1.getInt("ID");
			if (parentid == 0) {//如果父类ID为0,表示插入的是1级类别
				try {

					String new_sort_path = "0," + id1 + ",";//设置类的path为:"0,"+"自己的ID"+","
					String sqlUpdate2 = "update wptype set path='"//插入类别的path, 类别的depth为1.
						+ new_sort_path + "',depth=1 where id='"
						+ id1 + "'";
					dbc1.executeUpdate(sqlUpdate2);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} 
			else {//如果父类ID不为0,表示插入的是某个类别的子类.
				DBConnect dbc2 = new DBConnect();
				ResultSet rs2 = null;
				try {
					//查询数据库,找出所选择的父类
					String sqlSelect2 = "select * from wptype where ID=" + parentid+ "";
					rs2 = dbc2.executeQuery(sqlSelect2);

					if(rs2.next()){
						String latest_sort_path = rs2.getString("path")
						+ id1 + ","; 
						int latest_sort_depth = rs2.getInt("depth") + 1;//插入类别的depth为:父类的depth+1
						String sqlUpdate2 = "update wptype set path='"//更新(实际上是插入)类别的path和depth,
							+ latest_sort_path + "',depth="
							+ latest_sort_depth + " where ID='" + id1
							+ "'";
						dbc2.executeUpdate(sqlUpdate2);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				dbc2.close();
			}
		}
		dbc1.close();		
	}

	public void Edit(HttpServletRequest request) throws Exception {//生成一个request对象,传入上个页面的数据
		ID=StrFun.getInt(request,"id"); //为啥是小写id
		wptypename=StrFun.getString(request,"wptypename");         
		DBConnect dbc = new DBConnect(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		String Str = "update wptype set wptypename='" + wptypename+ "' where id=" + ID;
		dbc.executeUpdate(Str);
		String update_sp = "update wpb set wptypename='" + wptypename+ "' where wptypeID=" + ID;
		dbc.executeUpdate(update_sp);
		dbc.close();
	}

	public void Del() throws Exception {
		DBConnect dbc  = new DBConnect();
		try {
			String str = "select * from wptype where ID="+ID;
			ResultSet rs = dbc.executeQuery(str);
			if(rs.next()){
				int wptypeid = rs.getInt("ID");//必须先吧rs中的值赋值给一个参数,因为取出一次后再取出就会报null point exception错误
				//删除父类和父类下面的子类
				String sqlDel = "Delete from wptype where path like '%,"
					+ wptypeid + "%,'";
				dbc.executeUpdate(sqlDel);
				//删除类下面的所有商品
				String sqlDel_sp = "Delete from wpb where wppath like '%,"
					+ wptypeid + "%,'";
				dbc.executeUpdate(sqlDel_sp);
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		dbc.close();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -