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

📄 dbproduct.java

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

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

public class Dbproduct
{
	//执行查询的预执行语句
	private static final String LOAD_PRODUCT_BY_ID="SELECT * FROM [PRODUCT] WHERE [ID]=?";
	//执行插入的预执行语句
	private static final String INSERT_PRODUCT="INSERT INTO [PRODUCT]([Id],[Productname],[Productprice],[Productform],[Productnote],[Pic],[Status]) VALUES"
	 +"(?,?,?,?,?,?,?)";
	//负责更新的预执行语句
	private static final String UPDATE_PRODUCT="UPDATE [PRODUCT] SET [Productname]=?,[Productprice]=?,[Productform]=?,[Productnote]=?,[Status]=?"
	  +" WHERE [Id]=?";
	//执行删除的预执行语句
	private static final String Del_product="UPDATE [WARE] SET [Status]=? WHERE [Id]=?";
	//定义类使用的变量信息
	private int Id;
	private String Productname;
	private String Productprice;
	private String Productform;
	private String Productnote;
	private String Pic;
	private int Status;
	//使用构造函数进行对数据信息的操作
	public Dbproduct()
	{
	}
	//获取前台信息进行操作
	public Dbproduct(HttpServletRequest request)
	{
		this.Id=DbSequenceManager.nextID(FinalConstants.T_PRODUCT);
		this.Productname=ParamUtils.getEscapeHTMLParameter(request,"productname");
		this.Productprice=ParamUtils.getEscapeHTMLParameter(request,"productprice");
		this.Productform=ParamUtils.getEscapeHTMLParameter(request,"productform");
		this.Productnote=ParamUtils.getEscapeHTMLParameter(request,"productnote");
		this.Pic=ParamUtils.getEscapeHTMLParameter(request,"pic");
		this.Status=FinalConstants.STATUS_NORMAL;
		insertIntoDb();
	}
	
	//获取Id信息,进行提取信息的操作
	public Dbproduct(int Id) throws ProductNotFoundException
	{
		this.Id=Id;
		if(Id<=0)
		{
			return;
		}
		loadFromDb();
	}
	
	//删除操作
	protected void delete()
	{
		setStatus(FinalConstants.STATUS_DELETE);
	}
	//getxxx(),临时信息的存储
	public int getId()
	{
		return this.Id;
	}
	public String getProductname()
	{
		return this.Productname;
	}
	public String getProductprice()
	{
		return this.Productprice;
	}
	public String getProductform()
	{
		return this.Productform;
	}
	public String getProductnote()
	{
		return this.Productnote;
	}
	public String getPic()
	{
		return this.Pic;
	}
	public int getStatus()
	{
		return this.Status;
	}
	//setxxx()
	public void setId(int Id)
	{
		this.Id=Id;
		saveToDb();
	}
	public void setProductname(String Productname)
	{
		this.Productname=Productname;
		saveToDb();
	}
	public void setProductprice(String Productprice)
	{
		this.Productprice=Productprice;
		saveToDb();
	}
	public void setProductform(String Productform)
	{
		this.Productform=Productform;
		saveToDb();
	}
	public void setProdcutnote(String Productnote)
	{
		this.Productnote=Productnote;
		saveToDb();
	}
	public void setPic(String Pic)
	{
		this.Pic=Pic;
		saveToDb();
	}
	public void setStatus(int Status)
	{
		this.Status=Status;
		saveToDb();
	}
	
	//使用方法modify,传递参数HttpServletRequest类型,进行修改操作
	public void modify(HttpServletRequest request)
	{
		this.Id=ParamUtils.getIntParameter(request,"Id");
		this.Productname=ParamUtils.getEscapeHTMLParameter(request,"productname");
		this.Productprice=ParamUtils.getEscapeHTMLParameter(request,"productprice");
		this.Productform=ParamUtils.getEscapeHTMLParameter(request,"productform");
		this.Productnote=ParamUtils.getEscapeHTMLParameter(request,"productnote");
		this.Status=FinalConstants.STATUS_NORMAL;
		saveToDb();
	}
	//使用方法insertIntoDb,将数据信息进行插入操作
	private void insertIntoDb()
	{
		Connection con=null;
		PreparedStatement pstmt=null;
		try
		{
			con=DbConnectionManager.getConnection();
			pstmt=con.prepareStatement(INSERT_PRODUCT);
			pstmt.setInt(1,this.Id);
			pstmt.setString(2,StringUtils.toChinese(this.Productname));
			pstmt.setString(3,StringUtils.toChinese(this.Productprice));
			pstmt.setString(4,StringUtils.toChinese(this.Productform));
			pstmt.setString(5,StringUtils.toChinese(this.Productnote));
			pstmt.setString(6,StringUtils.toChinese(this.Pic));
			pstmt.setInt(7,this.Status);
			pstmt.executeQuery();
		}
		catch(SQLException sqle)
		{
			System.out.println("错误位置:DbProduct():insertIntoDb()"+sqle);
			sqle.printStackTrace();
		}
		finally
		{
			//关闭连接,释放资源
			try
			{
				pstmt.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			try
			{
				con.close();
			}
			catch(Exception ee)
			{
				ee.printStackTrace();
			}
		}
	}
	
	//使用方法loadFromDb检索出特定的数据信息
	private void loadFromDb() throws ProductNotFoundException
	{
		Connection con=null;
		PreparedStatement pstmt=null;
		try
		{
			con=DbConnectionManager.getConnection();
			pstmt=con.prepareStatement(LOAD_PRODUCT_BY_ID);
			pstmt.setInt(1,Id);
			ResultSet rs=pstmt.executeQuery();
			if(!rs.next())
			{
				throw new ProductNotFoundException("从数据库表[PRODUCT]中读取用户数据失败,欲读取的用户[ID]:["+Id+"]!");
			}
			this.Id=rs.getInt("Id");
			this.Productname=rs.getString("Productname");
			this.Productprice=rs.getString("Productprice");
			this.Productform=rs.getString("Productform");
			this.Productnote=rs.getString("Productnote");
			this.Pic=rs.getString("Pic");
			this.Status=rs.getInt("Status");
			
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		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_PRODUCT);
			//对数据库操作时,进行中文转换处理
			pstmt.setString(1,StringUtils.toChinese(this.Productname));
			pstmt.setString(2,StringUtils.toChinese(this.Productprice));
			pstmt.setString(3,StringUtils.toChinese(this.Productform));
			pstmt.setString(4,StringUtils.toChinese(this.Productnote));
			pstmt.setInt(5,this.Status);
			pstmt.setInt(6,this.Id);
			pstmt.executeUpdate();
		}
		catch(SQLException sqle)
		{
			System.err.println("错误位置:DbProduct.java:saveToDb():"+sqle);
			sqle.printStackTrace();
		}
		finally
		{
			try
			{
				pstmt.close();
				con.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
	}
	
	//使用方法DELToDb进行删除操作
	private void DELToDb()
	{
		Connection con=null;
		PreparedStatement pstmt=null;
		try
		{
		   con=DbConnectionManager.getConnection();
		   pstmt=con.prepareStatement(Del_product);
		   System.out.println(Del_product);
		   pstmt.setInt(1,this.Status);
		   pstmt.setInt(2,this.Id);
		   pstmt.executeUpdate();	
		}
		catch(SQLException sqle)
		{
		   System.out.print("错误位置:DbShop.java:DELToDb():"+sqle);
		   sqle.printStackTrace();
		}
		finally
		{
			try
			{
			   pstmt.close();
			   con.close();	
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String args[]) throws ProductNotFoundException
	{
		int id=20075001;
		try
		{
		  Dbproduct db=new Dbproduct(id);
		  System.out.println(db.getPic());
		  System.out.println(db.getProductform());		
		}
		catch(Exception e)
		{
	      e.printStackTrace();
		}		
	}
}

⌨️ 快捷键说明

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