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

📄 dbshop.java

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

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

public class Dbshop
{
	//将执行语句设置为final
	private static final String LOAD_SHOP_BY_ID="SELECT * FROM [USER] WHERE [ID]=?";
	//执行插入操作的预执行语句
	private static final String INSERT_SHOP="INSERT INTO [USER]([Id],[Username],[Password],"
	  +"[Shopname],[Area],[Peopleman],[Mantel],[Shoptel],[Usertype],[Createdate],[Status]) VALUES"+"(?,?,?,?,?,?,?,?,?,?,?)";
	//执行更新操作的预执行语句
	private static final String UPDATE_SHOP="UPDATE [USER] SET [Username]=?,[Password]=?,"
	  +"[Shopname]=?,[Area]=?,[Peopleman]=?,[Mantel]=?,[Shoptel]=?,[Usertype]=?,[Status]=? "
	  +"WHERE [Id]=?";
    //执行删除操作的预执行语句
    private static final String Del_SHOP="UPDATE [USER] SET [Status]=? WHERE [Id]=?";
    //定义类中使用的变量信息
    private int Id;
    private String Username;
    private String Password;
    private String Shopname;
    private String Area;
    private String Peopleman;
    private String Mantel;
    private String Shoptel;
    private int Usertype;
    private String Createdate;
    private int Status;
    
    public Dbshop()
    {
    }
    //使用构造函数获取信息,执行插入操作
    public Dbshop(HttpServletRequest request)
    {
    	this.Id=DbSequenceManager.nextID(FinalConstants.T_SHOP);
    	this.Username=ParamUtils.getEscapeHTMLParameter(request,"username");
    	this.Password=ParamUtils.getEscapeHTMLParameter(request,"password");
    	this.Shopname=ParamUtils.getEscapeHTMLParameter(request,"shopname");
    	this.Area=ParamUtils.getEscapeHTMLParameter(request,"area");
    	this.Peopleman=ParamUtils.getEscapeHTMLParameter(request,"peopleman");
    	this.Mantel=ParamUtils.getEscapeHTMLParameter(request,"mantel");
    	this.Shoptel=ParamUtils.getEscapeHTMLParameter(request,"shoptel");
    	this.Usertype=ParamUtils.getIntParameter(request,"usertype");
    	//设置日期格式,使用类simpleDateFormat设置格式
    	SimpleDateFormat simpleDate=new SimpleDateFormat("yyyyMMdd");
    	String shiftDateToDate=simpleDate.format(new java.util.Date());
    	this.Createdate=shiftDateToDate;
    	this.Status=FinalConstants.STATUS_NORMAL;
    	insertIntoDb();
    }
    
    //使用构造函数,查询符合要求的结果集中的信息
    public Dbshop(int Id) throws ShopNotFoundException
    {
    	this.Id=Id;
    	if(Id<=0)
    	{
    		return;
    	}
    	loadFromDb();
    }
    
    //执行删除操作
    protected void delete()
    {
    	setStatus(FinalConstants.STATUS_DELETE);
    }
    
    //getxxx()
    public int getId()
    {
    	return this.Id;
    }
    public String getUsername()
    {
    	return this.Username;
    }
    public String getPassword()
    {
    	return this.Password;
    }
    public String getShopname()
    {
    	return this.Shopname;
    }
    public String getArea()
    {
    	return this.Area;
    }
    public String getPeopleman()
    {
    	return this.Peopleman;
    }
    public String getMantel()
    {
    	return this.Mantel;
    }
    public String getShoptel()
    {
    	return this.Shoptel;
    }
    public int getUsertype()
    {
    	return this.Usertype;
    }
    public String getCreatedate()
    {
    	return this.Createdate;
    }
    public int getStatus()
    {
    	return this.Status;
    } 
    //setXXX() 方法
    public void setId(int Id)
    {
    	this.Id=Id;
    	saveToDb();
    }
    public void setUsername(String Username)
    {
    	this.Username=Username;
    	saveToDb();
    }
    public void setPassword(String Password)
    {
    	this.Password=Password;
    	saveToDb();
    }
    public void setShopname(String Shopname)
    {
    	this.Shopname=Shopname;
    	saveToDb();
    }
    public void setArea(String Area)
    {
    	this.Area=Area;
    	saveToDb();
    }
    public void setPeopleman(String Peopleman)
    {
    	this.Peopleman=Peopleman;
    	saveToDb();
    }
    public void setMantel(String Mantel)
    {
    	this.Mantel=Mantel;
    	saveToDb();
    }
    public void setShoptel(String Shoptel)
    {
    	this.Shoptel=Shoptel;
    	saveToDb();
    }
    public void setUsertype(int Usertype)
    {
    	this.Usertype=Usertype;
    	saveToDb();
    }
    public void setCreatedate(String Createdate)
    {
    	this.Createdate=Createdate;
    	saveToDb();
    }
    public void setStatus(int Status)
    {
    	this.Status=Status;
    	DELToDb();
    }
    
    //在类中定义modify方法,参数为HttpServletRequest,提取前台信息,进行修改操作
    public void modify(HttpServletRequest request)
    {
    	this.Id=ParamUtils.getIntParameter(request,"id");
    	this.Username=ParamUtils.getEscapeHTMLParameter(request,"username");
    	this.Password=ParamUtils.getEscapeHTMLParameter(request,"password");
    	this.Shopname=ParamUtils.getEscapeHTMLParameter(request,"shopname");
    	this.Area=ParamUtils.getEscapeHTMLParameter(request,"area");
    	this.Peopleman=ParamUtils.getEscapeHTMLParameter(request,"peopleman");
    	this.Mantel=ParamUtils.getEscapeHTMLParameter(request,"mantel");
    	this.Shoptel=ParamUtils.getEscapeHTMLParameter(request,"shoptel");
    	this.Usertype=ParamUtils.getIntParameter(request,"usertype");
    	this.Status=FinalConstants.STATUS_NORMAL;
    	saveToDb();
    }
    
    //定义insertIntoDb()方法进行插入数据信息的操作
    private void insertIntoDb()
    {
    	Connection con=null;
    	PreparedStatement pstmt=null;
    	try
    	{
    		con=DbConnectionManager.getConnection();
    		pstmt=con.prepareStatement(INSERT_SHOP);
    		pstmt.setInt(1,this.Id);
    		pstmt.setString(2,StringUtils.toChinese(this.Username));
    		pstmt.setString(3,StringUtils.toChinese(this.Password));
    		pstmt.setString(4,StringUtils.toChinese(this.Shopname));
    		pstmt.setString(5,StringUtils.toChinese(this.Area));
    		pstmt.setString(6,StringUtils.toChinese(this.Peopleman));
    		pstmt.setString(7,StringUtils.toChinese(this.Mantel));
    		pstmt.setString(8,StringUtils.toChinese(this.Shoptel));
    		pstmt.setInt(9,this.Usertype);
    		pstmt.setString(10,StringUtils.toChinese(this.Createdate));
    		pstmt.setInt(11,this.Status);
    		pstmt.executeUpdate();    		
    	}
    	catch(SQLException sqle)
    	{
    		System.out.println("错误位置:DbShop:insertIntoDb()"+sqle);
    		sqle.printStackTrace();
    	}
    	finally
    	{
    		//将连接关闭,释放资源
    		try
    		{
    			pstmt.close();
    			con.close();
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    
    //使用loadFromDb方法进行查询操作
    private void loadFromDb() throws ShopNotFoundException
    {
    	Connection con=null;
    	PreparedStatement pstmt=null;
    	try
    	{
    		con=DbConnectionManager.getConnection();
    		pstmt=con.prepareStatement(LOAD_SHOP_BY_ID);
    		pstmt.setInt(1,Id);
    		ResultSet rs=pstmt.executeQuery();
    		if(!rs.next())
    		{
    		    throw new ShopNotFoundException("从数据表[SHOP]中读取用户数据失败,预读取的用户第:ID["
    		      +Id+"]!");
    		}
    		this.Id=rs.getInt("Id");
    		this.Username=rs.getString("Username");
    		this.Password=rs.getString("Password");
    		this.Shopname=rs.getString("Shopname");
    		this.Area=rs.getString("Area");
    		this.Peopleman=rs.getString("Peopleman");
    		this.Mantel=rs.getString("Mantel");
    		this.Shoptel=rs.getString("Shoptel");
    		this.Usertype=rs.getInt("Usertype");
    		this.Createdate=rs.getString("Createdate");
    		this.Status=rs.getInt("Status");
    	}
    	catch(SQLException sqle)
    	{
    		 throw new ShopNotFoundException("从数据表[SHOP]中读取用户数据失败,预读取的用户第: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_SHOP);
      	pstmt.setString(1,StringUtils.toChinese(this.Username));
      	pstmt.setString(2,StringUtils.toChinese(this.Password));
      	pstmt.setString(3,StringUtils.toChinese(this.Shopname));
      	pstmt.setString(4,StringUtils.toChinese(this.Area));
      	pstmt.setString(5,StringUtils.toChinese(this.Peopleman));
      	pstmt.setString(6,StringUtils.toChinese(this.Mantel));
      	pstmt.setString(7,StringUtils.toChinese(this.Shoptel));
      	pstmt.setInt(8,this.Usertype);
      	pstmt.setInt(9,this.Status);
      	pstmt.setInt(10,this.Id);
      	pstmt.executeUpdate();      	
      }	
      catch(SQLException sqle)
      {
      	System.err.println("错误位置:DbShop.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_SHOP);
    	    pstmt.setInt(1,this.Status);
    	    pstmt.setInt(2,this.Id);
    	    pstmt.executeUpdate();	
    	}
    	catch(SQLException sqlee)
    	{
    		System.err.println("错误位置:DbShop.java:DELToDb():"+sqlee);
      	    sqlee.printStackTrace();
    	}
    	finally
    	{
    		try
    		{
    		   pstmt.close();
    		   con.close();	
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    
    public static void main(String args[]) throws ShopNotFoundException
    {
    	int Id=20074001;
    	Dbshop db=new Dbshop(Id);
    	System.out.println(db.getId());
    	System.out.println(db.getShopname());
    	System.out.println(db.getArea());
    	System.out.println(db.getMantel());
    	System.out.println(db.getPassword());
    	System.out.println(db.getPeopleman());
    	System.out.println(db.getCreatedate());
    	System.out.println(db.getShoptel());
    	System.out.println(db.getStatus());
    	System.out.println(db.getUsername());
    	System.out.println(db.getUsertype());   	
    }
}

⌨️ 快捷键说明

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