小米商城java web项目,jsp+servlet开发java web小米商城项目

这是一个Java编程实现的DAO(Data Access Object)类,用于与数据库进行交互。类中包含了根据typeID、productID和productname查找商品的方法,以及获取分页数据和总数的方法。这些方法使用了PreparedStatement进行SQL查询,确保了代码的安全性。
摘要由CSDN通过智能技术生成

package dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import model.Cart;

import model.Product;

import db.DBManager;

public class ProductDAO {

//根据typeID找到分类商品

public List getProductByTypeId(int typeID){

Connection conn = DBManager.getConn();

PreparedStatement pstmt = null;

String sql = null;

List product = new ArrayList();

try {

try {

sql = "select * from product where typeID=?";

pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, typeID);

ResultSet rs = pstmt.executeQuery();

while(rs.next())

{

Product p=new Product();

p.setProductID(rs.getInt("productID"));

p.setProductname(rs.getString("productname"));

p.setPrice(rs.getFloat("price"));

p.setDiscribe(rs.getString("discribe"));

p.setImage(rs.getString("Image"));

p.setTypeID(rs.getInt("typeID"));

p.setDiscribe2(rs.getString("discribe2"));

p.setColor(rs.getString("Color"));

p.setNum(rs.getInt("num"));

p.setEdition(rs.getString("edition"));

product.add(p);

}

}finally {

conn.close();

}

} catch (Exception e) {

e.printStackTrace();

}

return product;

}

//根据productID找到此商品

public List getProductByproductID(int productID){

Connection conn = DBManager.getConn();

PreparedStatement pstmt = null;

String sql = null;

List product = new ArrayList();

try {

try {

sql = "select * from product where productID=?";

pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, productID);

ResultSet rs = pstmt.executeQuery();

while(rs.next())

{

Product p=new Product();

p.setProductID(rs.getInt("productID"));

p.setProductname(rs.getString("productname"));

p.setPrice(rs.getFloat("price"));

p.setDiscribe(rs.getString("discribe"));

p.setImage(rs.getString("Image"));

p.setTypeID(rs.getInt("typeID"));

p.setDiscribe2(rs.getString("discribe2"));

p.setColor(rs.getString("Color"));

p.setNum(rs.getInt("num"));

p.setEdition(rs.getString("edition"));

product.add(p);

}

}finally {

conn.close();

}

} catch (Exception e) {

e.printStackTrace();

}

return product;

}

//根据productname找到此商品

public List getProductByproductname(String searchname){

Connection conn = DBManager.getConn();

PreparedStatement pstmt = null;

String sql = null;

List product = new ArrayList();

try {

try {

sql = "select * from product where productname=?";

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, searchname);

ResultSet rs = pstmt.executeQuery();

while(rs.next())

{

Product p=new Product();

p.setProductID(rs.getInt("productID"));

p.setProductname(rs.getString("productname"));

p.setPrice(rs.getFloat("price"));

p.setDiscribe(rs.getString("discribe"));

p.setImage(rs.getString("Image"));

p.setTypeID(rs.getInt("typeID"));

p.setDiscribe2(rs.getString("discribe2"));

p.setColor(rs.getString("Color"));

p.setNum(rs.getInt("num"));

p.setEdition(rs.getString("edition"));

product.add(p);

}

}finally {

conn.close();

}

} catch (Exception e) {

e.printStackTrace();

}

return product;

}

///**

// * 查询数据库中部分数据 参数1:开始位置,从0开始 参数2:几条记录

// */

public List getPageList (int begin, int count){

Connection conn = DBManager.getConn();

PreparedStatement pstmt = null;

List products = new ArrayList();

String sql = "select * from product limit ?,?";

try {

pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, begin);

pstmt.setInt(2, count);

ResultSet rs = pstmt.executeQuery();

while(rs.next())

{

Product p=new Product();

p.setProductID(rs.getInt("productID"));

p.setProductname(rs.getString("productname"));

p.setPrice(rs.getFloat("price"));

p.setDiscribe(rs.getString("discribe"));

p.setImage(rs.getString("Image"));

p.setTypeID(rs.getInt("typeID"));

p.setDiscribe2(rs.getString("discribe2"));

p.setColor(rs.getString("Color"));

p.setNum(rs.getInt("num"));

p.setEdition(rs.getString("edition"));

products.add(p);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return products;

}

///**

// * 查询表中的记录总数

// */

public int getSum(){

int count = 0 ;

PreparedStatement pstmt = null;

String sql = "select count(*) from product";

Connection conn = DBManager.getConn();

try {

pstmt = conn.prepareStatement(sql);

ResultSet rs = pstmt.executeQuery();

while(rs.next())

{

count = rs.getInt(1);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return count;

}

///**

// * 方法2/查询所有

// */

public List getAllProducts() {

List products = new ArrayList();

PreparedStatement pstmt = null;

String sql = "select * from product";

Connection conn = DBManager.getConn();

try {

pstmt = conn.prepareStatement(sql);

ResultSet rs = pstmt.executeQuery();

while(rs.next())

{

Product p=new Product();

p.setProductID(rs.getInt("productID"));

p.setProductname(rs.getString("productname"));

p.setPrice(rs.getFloat("price"));

p.setDiscribe(rs.getString("discribe"));

p.setImage(rs.getString("Image"));

p.setTypeID(rs.getInt("typeID"));

p.setDiscribe2(rs.getString("discribe2"));

p.setColor(rs.getString("Color"));

p.setNum(rs.getInt("num"));

p.setEdition(rs.getString("edition"));

products.add(p);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return products;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值