productdao.java
来自「java编写的网络购物简洁版」· Java 代码 · 共 482 行
JAVA
482 行
package shop;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.sql.*;
public class productDAO implements chapinInterface {
DBTools dao = new DBTools();
/* (非 Javadoc)
* @see shop.chapinInterface#addShanpin(shop.productBean)
*/
public boolean addShanpin(productBean s){
Connection con = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"insert into product(breedID,productName,factory,price,amout,remark) values(?,?,?,?,?,?)");
pstmt.setInt(1,s.getBreedID());
pstmt.setString(2,s.getProductName());
pstmt.setString(3,s.getFactory());
pstmt.setFloat(4,s.getPrice());
pstmt.setInt(5,s.getAmout());
pstmt.setString(6,s.getRemark());
int rows = pstmt.executeUpdate();
if(rows>0)
{
flag = true;
}
else
{
flag = false;
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
dao.close(con,pstmt);
}
return flag;
}
/* (非 Javadoc)
* @see shop.chapinInterface#updateShanpin(shop.productBean)
*/
public boolean updateShanpin(productBean s){
Connection con = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"update product set breedID=?,productName = ?,factory = ? ,price=? ,amout=?,remark=? where productID = ?");
pstmt.setInt(1,s.getBreedID());
pstmt.setString(2,s.getProductName());
pstmt.setString(3,s.getFactory());
pstmt.setFloat(4,s.getPrice());
pstmt.setInt(5,s.getAmout());
pstmt.setString(6,s.getRemark());
pstmt.setInt(7,s.getProductID());
int rows = pstmt.executeUpdate();
if(rows>0)
{
flag = true;
}
else
{
flag = false;
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
dao.close(con, pstmt);
}
return flag;
}
/* (非 Javadoc)
* @see shop.chapinInterface#delShangpin(int)
*/
public boolean delShangpin(int productid){
Connection con = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"delete from product where productID = ?");
pstmt.setInt(1,productid);
int rows = pstmt.executeUpdate();
if(rows>0)
{
flag = true;
}
else
{
flag = false;
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
dao.close(con, pstmt);
}
return flag;
}
/* (非 Javadoc)
* @see shop.chapinInterface#getAllShanpin()
*/
public Collection getAllShanpin() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Collection <productBean> studentList = new ArrayList<productBean>();
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"SELECT productID, breedID,productName, factory, price,amout,remark FROM product ");
rs = pstmt.executeQuery();
while(rs.next())
{
productBean s = new productBean(); //学生实体类
s.setProductID(rs.getInt(1));
s.setBreedID(rs.getInt(2));
s.setProductName(rs.getString(3));
s.setFactory(rs.getString(4));
s.setPrice(rs.getFloat(5));
s.setAmout(rs.getInt(6));
s.setRemark(rs.getString(7));
studentList.add(s);
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
dao.close(con, pstmt, rs);
}
return studentList;
}
/* (非 Javadoc)
* @see shop.chapinInterface#selectchanpin(int)
*/
public productBean selectchanpin(int s)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
//Collection breedNameList = new ArrayList();
productBean pBean = new productBean();
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"select * from product where productID = ?");
pstmt.setInt(1,s);
rs = pstmt.executeQuery();
if(rs.next())
{
pBean.setProductID(rs.getInt(1));
pBean.setBreedID(rs.getInt(2));
pBean.setProductName(rs.getString(3));
pBean.setFactory(rs.getString(4));
pBean.setPrice(rs.getFloat(5));
pBean.setAmout(rs.getInt(6));
pBean.setRemark(rs.getString(7));
}
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
dao.close(con, pstmt, rs);
}
return pBean;
}
/* (非 Javadoc)
* @see shop.chapinInterface#selectproduct(java.lang.String)
*/
public Collection selectproduct(String s)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Collection <productBean> productNameList = new ArrayList<productBean>();
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"SELECT product.productName FROM product INNER JOIN breed ON product.breedID = breed.breedID WHERE breed.breedName = ?");
pstmt.setString(1,s);
rs = pstmt.executeQuery();
while(rs.next())
{
productBean pBean = new productBean();
pBean.setProductName(rs.getString(1));
productNameList.add(pBean);
}
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
dao.close(con, pstmt, rs);
}
return productNameList;
}
/* (非 Javadoc)
* @see shop.chapinInterface#delShangpin(java.lang.String)
*/
public boolean delShangpin(String name){
Connection con = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"delete from product where productName = ?");
pstmt.setString(1,name);
int rows = pstmt.executeUpdate();
if(rows>0)
{
flag = true;
}
else
{
flag = false;
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
dao.close(con, pstmt);
}
return flag;
}
/**
* 得到数据库中所有的商品信息。
*/
public Collection getproduct() throws Exception
{
Connection con=null;
Statement stmt = null;
ResultSet rs=null;
ArrayList<productBean> productList=new ArrayList<productBean>();
try
{
con = dao.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select * from product");
while (rs.next())
{
productBean book = new productBean(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getFloat(5),rs.getInt(6),rs.getString(7));
productList.add(book);
}
return productList;
}
finally
{
dao.close(con,stmt,rs);
}
}
/**
*得到选择的商品信息。
*/
public productBean getproduct(int productId) throws SQLException
{
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try
{
con = dao.getConnection();
pstmt = con.prepareStatement("select * from product where productID = ?");
pstmt.setInt(1,productId);
rs=pstmt.executeQuery();
productBean product=null;
if(rs.next())
{
product = new productBean(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getFloat(5),rs.getInt(6),rs.getString(7));
}
return product;
}
finally
{
dao.close(con,pstmt,rs);
}
}
/**
* 通过关键字搜索商品的信息。
*/
public Collection searchBook(String keyword) throws SQLException
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
ArrayList<productBean> bookList=new ArrayList<productBean>();
try
{
con = dao.getConnection();
stmt = con.createStatement();
String sql="select * from product where productName like '%"+keyword+"%'";
rs=stmt.executeQuery(sql);
while (rs.next())
{
productBean product = new productBean(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getFloat(5),rs.getInt(6),rs.getString(7));
bookList.add(product);
}
return bookList;
}
finally
{
dao.close(con,stmt,rs);
}
}
/**
* 功能: 判断剩余的图书的数量是否大于客户购买的数量。
*
* @param bookId 图书ID
* @param quantity 图书数量
* @return
* @throws SQLException
*/
public boolean isAmountEnough(int productId, int quantity) throws SQLException
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
boolean bEnough=false;
try
{
con = dao.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select amout from product where productID = " + productId);
while(rs.next())
{
int amount=rs.getInt(1);
if(amount >= quantity)
bEnough=true;
}
}
finally
{
dao.close(con,stmt,rs);
}
return bEnough;
}
/**
* 功能:购买购物车中所有的图书。
*
* @param cart 购物车
*
* @throws SQLException
*/
public void buyBooks(CartBean cart) throws SQLException
{
Connection con = null;
PreparedStatement pstmt = null;
Iterator it =cart.getItems().iterator();
try
{
con = dao.getConnection();
con.setAutoCommit(false); //设置为手动提交事务
String sql="update prodoct set amount = amount - ? where productID = ?";
pstmt=con.prepareStatement(sql);
while(it.hasNext())
{
CartItemBean item=(CartItemBean)it.next();
productBean product=item.getproduct();
int bookId=product.getProductID();
int quantity=item.getQuantity();
pstmt.setInt(1,quantity);
pstmt.setInt(2,bookId);
pstmt.addBatch();
}
pstmt.executeBatch(); //执行批处理
con.commit(); //提交事务
}catch(SQLException sqle)
{
con.rollback(); //回滚事务
}
finally
{
dao.close(con,pstmt);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?