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

📄 productbean.java

📁 简易商品管理系统代码
💻 JAVA
字号:
package jdbcbook.ch08;
import java.sql.*;
import java.util.*;
import jdbcbook.pub.util.*;
/*
 * 商品处理bean
 */
public class ProductBean
{
	//增加商品信息
	public boolean addProduct(Product product)
	{
		if(product==null||StringUtil.isEmptyString(product.getName())||StringUtil.isEmptyString(product.getManufacturer())||product.getCategoryid()<=0)
			return false;
		Connection conn=null;
		Statement st=null;
		try
		{
			conn=DatabaseBean.getConnection();
			st=conn.createStatement();
			//增加商品信息到数据库
			String sql="insert into product(productid,name,manufacturer,description,categoryid) values('"+DatabaseBean.getMaxID("product")+"','"+product.getName()+"','"+product.getManufacturer()+"','"+product.getDescription()+"','"+product.getCategoryid()+"')";
			int nResult=st.executeUpdate(sql);
			return nResult==1;
		}
		catch(SQLException e)
		{
			e.printStackTrace(System.err);
			return false;
		}
		finally
		{
			DatabaseBean.close(null, st, conn);
		}
	}
	//查询商品信息
	public Vector queryProduct(Product product)
	{
		Vector vt=new Vector();
		Connection conn=null;
		Statement st=null;
		ResultSet rs=null;
		try
		{
			conn=DatabaseBean.getConnection();
			st=conn.createStatement();
			String sql="select productid,name,manufacturer,description,categoryid from product";
			//如果输入名称,则模糊查询,否者查询的所有
			if(product!=null&&StringUtil.isEmptyString(product.getName()))
			{
				sql=sql+" where name like '% "+product.getName()+" %'";
			}
			if(product!=null&&product.getCategoryid()>0)
			{
				if(sql.indexOf("where")>0)
					sql=sql+" and categoryid='"+product.getCategoryid()+"'";
				else
					sql=sql+" where catrgoryid='"+product.getCategoryid()+"'";
			}
			//增加排序规则
			sql=sql+" order by name asc";
			
			rs=st.executeQuery(sql);
			while(rs.next())
			{
				Product p=new Product();
				p.setProductID(rs.getInt(1));
				p.setName(rs.getString(2));
				p.setManmfacturer(rs.getString(3));
				p.setDescription(rs.getString(4));
				p.setCategoryid(rs.getInt(5));
				vt.add(p);
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace(System.err);
		}
		finally
		{
			DatabaseBean.close(rs, st, conn);
		}
		return vt;
	}
	//获得商品信息
	public Product getProduct(Product product)
	{
		if(product.getProductID()<=0)
			return null;
		Product p=null;
		Connection conn=null;
		Statement st=null;
		ResultSet rs=null;
		try
		{
			conn=DatabaseBean.getConnection();
			st=conn.createStatement();
			String sql="select productid,name,manufacturer,description,categoryid from product where productid='"+product.getProductID()+"'";
			rs=st.executeQuery(sql);
			while(rs.next())
			{
				p=new Product();
				p.setProductID(rs.getInt(1));
				p.setName(rs.getString(2));
				p.setManmfacturer(rs.getString(3));
				p.setDescription(rs.getString(4));
				p.setCategoryid(rs.getInt(5));
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace(System.err);
		}
		finally
		{
			DatabaseBean.close(rs, st, conn);
		}
		return p;
	}
	//修改商品信息
	public boolean updateProduct(Product product)
	{
		if(product==null||StringUtil.isEmptyString(product.getName())||StringUtil.isEmptyString(product.getManufacturer())||product.getCategoryid()<=0||product.getProductID()<=0)
			return false;
		Connection conn=null;
		Statement st=null;
		try
		{
			conn=DatabaseBean.getConnection();
			st=conn.createStatement();
			//修改商品信息到数据库
			String sql="update product set name='"+product.getName()+"',manufacturer='"+product.getManufacturer()+"',description='"+product.getDescription()+"',categoryid='"+product.getCategoryid()+"' where productid='"+product.getProductID()+"'";
			int nResult=st.executeUpdate(sql);
			return nResult==1;
		}
		catch(SQLException e)
		{
			e.printStackTrace(System.err);
			return false;
		}
		finally
		{
			DatabaseBean.close(null, st, conn);
		}
	}
	public boolean delProduct(Product product)
	{
		if(product.getProductID()<=0)
			return false;
		Connection conn=null;
		Statement st=null;
		try
		{
			//得到数据库连接
			conn=DatabaseBean.getConnection();
			st=conn.createStatement();
			//删除商品信息语句
			String sql="delete from product where productid='"+product.getProductID()+"'";
			//执行sql语句
			int nResult=st.executeUpdate(sql);
			return nResult==1;
		}
		catch(SQLException e)
		{
			e.printStackTrace(System.err);
			return false;
		}
		finally
		{
			DatabaseBean.close(null, st, conn);
		}
	}
	
	
	
}

⌨️ 快捷键说明

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