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

📄 dbware.java

📁 基于JSP的家电连锁超市管理系统 数据库也在文件内
💻 JAVA
字号:
package electric.dbs;

import java.sql.*;
import java.sql.SQLException;
import javax.servlet.http.*;
import electric.dbs.*;
import java.text.SimpleDateFormat;
import electric.electricUtils.*;

public class Dbware
{
	//将类中操作的sql语句定义为final型
	//进行查询的sql预执行语句
	private static final String LOAD_WARE_BY_ID="SELECT * FROM [WARE] WHERE [Id]=?";
	//进行插入操作的sql预执行语句
	private static final String INSERT_WARE="INSERT INTO [WARE] ([Id],[Pname],[Pmodel],[Pcost],[Pheft],"+
	   "[Pfacturer],[Pnote],[Createdate],[Status])VALUES"+"(?,?,?,?,?,?,?,?,?)";
	//进行更新操作的sql预执行语句
	private static final String UPDATE_WARE="UPDATE [WARE] SET [Pname]=?,[Pmodel]=?,[Pcost]=?,[Pheft]=?,"+
	   "[Pfacturer]=?,[Pnote]=?,[Status]=? WHERE [Id]=?";
	//执行删除操作的sql预执行语句
	private static final String DEL_WARE="UPDATE [WARE] SET [Status]=? WHERE [Id]=?";
	//声明在类中使用的变量名称
	  private int Id;
	  private String Pname;
	  private String Pmodel;
	  private String Pcost;
	  private String Pheft;
	  private String Pfacturer;
	  private String Pnote;
	  private String Createdate;
	  private int Status;
	//通过构造函数,将HttpServletRequest作为参数进行传递,获取前台提交的信息
    public Dbware(HttpServletRequest request)
    {
    	this.Id=DbSequenceManager.nextID(FinalConstants.T_WARE);
    	this.Pname=ParamUtils.getEscapeHTMLParameter(request,"pname");
    	this.Pmodel=ParamUtils.getEscapeHTMLParameter(request,"pmodel");
    	this.Pcost=ParamUtils.getEscapeHTMLParameter(request,"pcost");
    	this.Pheft=ParamUtils.getEscapeHTMLParameter(request,"pheft");
    	this.Pfacturer=ParamUtils.getEscapeHTMLParameter(request,"pfacturer");
    	this.Pnote=ParamUtils.getEscapeHTMLParameter(request,"pnote");
    	SimpleDateFormat simpleDate=new SimpleDateFormat("yyyyMMdd");
    	String shiftDateToDate=simpleDate.format(new java.util.Date());
    	this.Createdate=shiftDateToDate;
    	this.Status=FinalConstants.STATUS_NORMAL;
    	insertIntoDb();
    }
    public Dbware()
    {
    }
    public Dbware(int Id) throws WareNotFoundException
    {
    	this.Id=Id;
    	if(Id<=0)
    	{
    		return;
    	}
    	loadFromDb();
    }
    
    //获取Id信息的操作
    public static Dbware getInstance(int Id) throws WareNotFoundException
    {
    	return new Dbware(Id);
    }
    
    //getXXX()
  public int getId() {
    return this.Id;
  }

  public String getPname()
  {
    return this.Pname;
  }

  public String getPmodel()
  {
    return this.Pmodel;
  }

  public String getPcost()
  {
    return this.Pcost;
  }

  public String getPheft()
  {
    return this.Pheft;
  }

  public String getPfacturer()
  {
    return this.Pfacturer;
  }

  public String getPnote()
  {
    return this.Pnote;
  }
  public String getCreatedate()
 {
   return this.Createdate;
 }
  public int getStatus()
  {
    return this.Status;
  }


  //setXXX()
	  public void setId(int Id)
	  {
	    this.Id=Id;
	    saveToDb();
	  }
	  public void setPname(String Pname)
	  {
	    this.Pname=Pname;
	    saveToDb();
	  }
    public void setPmodel(String Pmodel)
    {
      this.Pmodel=Pmodel;
      saveToDb();
    }

    public void setPcost(String Pcost)
    {
      this.Pcost=Pcost;
      saveToDb();
    }

    public  void setPheft(String Pheft)
    {
      this.Pheft=Pheft;
      saveToDb();
    }

    public void setPfacturer(String Pfacturer)
    {
      this.Pfacturer=Pfacturer;
      saveToDb();
    }

    public void setPnote(String pnote)
    {
      this.Pnote=Pnote;
      saveToDb();
    }
    public void setCreatedate(String Createdate)
    {
    this.Createdate=Createdate;
    saveToDb();
    }
    public void setStatus(int status)
   {
    this.Status=status;
    saveToDb();
   }
   
   //方法modify负责对信息进行修改操作,起参数为HttpServletRequest
   public void modify(HttpServletRequest request)
   {
   	this.Id=ParamUtils.getIntParameter(request,"id");
   	this.Pname=ParamUtils.getEscapeHTMLParameter(request,"pname");
   	this.Pmodel=ParamUtils.getEscapeHTMLParameter(request,"pmodel");
   	
   	this.Pcost=ParamUtils.getEscapeHTMLParameter(request,"pcost");
   	this.Pheft=ParamUtils.getEscapeHTMLParameter(request,"pheft");
   	this.Pfacturer=ParamUtils.getEscapeHTMLParameter(request,"pfacturer");
   	this.Pnote=ParamUtils.getEscapeHTMLParameter(request,"pnote");
   	System.out.println("!!! modify  "+this.Pnote);
   	this.Status=FinalConstants.STATUS_NORMAL;
   	saveToDb();
   }
   
   //loadFromDb() 查询操作
   private void loadFromDb() throws WareNotFoundException
   {
   	 Connection con=null;
   	 PreparedStatement pstmt=null;
   	 try
   	 {
   	 	con=DbConnectionManager.getConnection();
   	 	pstmt=con.prepareStatement(LOAD_WARE_BY_ID);
   	 	pstmt.setInt(1,Id);
   	 	ResultSet rs=pstmt.executeQuery();
   	 	if(!rs.next())
   	 	{
   	 	  throw new WareNotFoundException("从数据表[WARE]中读取用户数据失败,欲读取的用户ID:"+Id+"]!");	
   	 	}
   	 	//将查询的结果保存到变量中
   	 	this.Id=rs.getInt("Id");
   	 	this.Pname=rs.getString("Pname");
   	 	this.Pmodel=rs.getString("Pmodel");
   	 	this.Pcost=rs.getString("Pcost");
   	 	this.Pheft=rs.getString("Pheft");
   	 	this.Pfacturer=rs.getString("Pfacturer");
   	 	this.Pnote=rs.getString("Pnote");
   	 	this.Createdate=rs.getString("Createdate");
   	 	this.Status=rs.getInt("Status"); 	 	
   	 }
   	 catch(SQLException sqle)
   	 {
   	 	throw new WareNotFoundException("从数据表[WARE]中读取用户数据失败,欲读取的用户ID:"+Id+"]!");
   	 }
   	 finally
   	 {
   	 	try
   	 	{
   	 	    pstmt.close();
   	 	    con.close();	
   	 	}
   	 	catch(Exception e)
   	 	{
   	 		e.printStackTrace();
   	 	}
   	 }
   }
   
   //saveToDb() 更新操作
   private void saveToDb()
   {
   	Connection con=null;
   	PreparedStatement pstmt=null;
   	try
   	{
   		con=DbConnectionManager.getConnection();
   		pstmt=con.prepareStatement(UPDATE_WARE);
   		
   		pstmt.setString(1,StringUtils.toChinese(this.Pname));
   		System.out.println("!!! Pname "+this.Pname);
   		pstmt.setString(2,StringUtils.toChinese(this.Pmodel));
   		pstmt.setString(3,StringUtils.toChinese(this.Pcost));
   		pstmt.setString(4,StringUtils.toChinese(this.Pheft));
   		pstmt.setString(5,StringUtils.toChinese(this.Pfacturer));
   		pstmt.setString(6,StringUtils.toChinese(this.Pnote));
   		pstmt.setInt(7,this.Status);
   		pstmt.setInt(8,this.Id);
   		pstmt.executeUpdate();
   	}
   	catch(SQLException sqle)
   	 {
   	 	System.out.println("错误位置:Dbware:saveToDb()-"+sqle);
   	 	sqle.printStackTrace();
   	 }
   	 finally
   	 {
   	 	try
   	 	{
   	 	    pstmt.close();
   	 	    con.close();	
   	 	}
   	 	catch(Exception e)
   	 	{
   	 		e.printStackTrace();
   	 	}
   	 }
   }
   
   //insertIntoDb() 插入操作
   private void insertIntoDb()
   {
   	  Connection con=null;
   	  PreparedStatement pstmt=null;
   	  try
   	  {
   	   	con=DbConnectionManager.getConnection();
   	   	pstmt=con.prepareStatement(INSERT_WARE);
   	   	pstmt.setInt(1,this.Id);
   		pstmt.setString(2,StringUtils.toChinese(this.Pname));
   		pstmt.setString(3,StringUtils.toChinese(this.Pmodel));
   		pstmt.setString(4,StringUtils.toChinese(this.Pcost));
   		pstmt.setString(5,StringUtils.toChinese(this.Pheft));
   		pstmt.setString(6,StringUtils.toChinese(this.Pfacturer));
   		pstmt.setString(7,StringUtils.toChinese(this.Pnote));
   		pstmt.setString(8,StringUtils.toChinese(this.Createdate));
   		pstmt.setInt(9,this.Status); 
   		pstmt.executeUpdate();  	   	
   	  }
   	  catch(SQLException sqle)
   	  {
   	 	System.out.println("错误位置:Dbware:insertIntoDb()-"+sqle);
   	 	sqle.printStackTrace();
   	  }
   	  finally
   	  {
   	 	try
   	 	{
   	 	    pstmt.close();
   	 	    con.close();	
   	 	}
   	 	catch(Exception e)
   	 	{
   	 		e.printStackTrace();
   	 	}
   	  }
   }
   
   public static void main(String args[]) throws WareNotFoundException
   {
   	int id=20073001;
   	Dbware db=new Dbware(id);
   	System.out.println(db.getId());
   	System.out.println(db.getPname());
   	System.out.println(db.getPcost());
   	System.out.println(db.getPfacturer());
   	System.out.println(db.getPheft());
   	System.out.println(db.getPmodel());
   	System.out.println(db.getPnote());
   	System.out.println(db.getCreatedate());
   	System.out.println(db.getStatus());	
   	
   }
    
}

⌨️ 快捷键说明

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