package com.bjsxt.shopping.product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.bjsxt.shopping.category.Category;
import com.bjsxt.shopping.util.DB;
public class ProductMySQLDAO implements ProductDAO {
public void add(Product p) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DB.getConn();
String sql = "insert into product values (null, ?, ?, ?, ?, ?, ?)";
pstmt = DB.prepare(conn, sql);
pstmt.setString(1, p.getName());
pstmt.setString(2, p.getDescr());
pstmt.setDouble(3, p.getNormalPrice());
pstmt.setDouble(4, p.getMemberPrice());
pstmt.setTimestamp(5, new Timestamp(p.getPdate().getTime()));
pstmt.setInt(6, p.getCategoryId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(pstmt);
DB.close(conn);
}
}
public void delete(int id) {
Connection conn = null;
Statement stmt = null;
String sql;
try {
conn = DB.getConn();
sql = "delete from product where id = " + id;
stmt = DB.getStatement(conn);
DB.executeUpdate(stmt, sql);
} finally {
DB.close(stmt);
DB.close(conn);
}
}
public List<Product> getProducts() {
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "select * from product order by pdate desc";
ResultSet rs = DB.getResultSet(stmt, sql);
List<Product> products = new ArrayList<Product>();
try {
while (rs.next()) {
Product p = getProductFromRs(rs);
products.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(stmt);
DB.close(rs);
DB.close(conn);
}
return products;
}
/**
* @param lazy 为true时,只取Product的信息,否则同时取出Product内Category对象的信息
*/
public int getProducts(List<Product> products, int pageNo, int pageSize, boolean lazy) {
int totalRecords = -1;
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "";
if(lazy) {
sql = "select * from product order by pdate desc";
} else {
sql = "select p.id productid, p.name pname, p.descr pdescr, p.normalprice, " +
" p.memberprice, p.pdate, p.categoryid , " +
" c.id categoryid, c.name cname, c.descr cdescr, c.pid, c.cno, c.grade " +
" from product p join category c on (p.categoryid = c.id) order by p.pdate desc";
}
sql += " limit " + (pageNo - 1) * pageSize + "," + pageSize;
ResultSet rs = DB.getResultSet(stmt, sql);
Statement stmtCount = DB.getStatement(conn);
ResultSet rsCount = DB.getResultSet(stmtCount,
"select count(*) from product");
//products = new ArrayList<Product>(); 千万小心这句话不要添加
try {
rsCount.next();
totalRecords = rsCount.getInt(1);
while (rs.next()) {
Product p = null;
if(lazy) {
p = this.getProductFromRs(rs);
} else {
p = new Product();
p.setId(rs.getInt("productid"));
p.setName(rs.getString("pname"));
p.setDescr(rs.getString("pdescr"));
p.setNormalPrice(rs.getDouble("normalprice"));
p.setMemberPrice(rs.getDouble("memberprice"));
p.setPdate(rs.getTimestamp("pdate"));
p.setCategoryId(rs.getInt("categoryid"));
Category c = new Category();
c.setId(rs.getInt("categoryid"));
c.setName(rs.getString("cname"));
c.setDescr(rs.getString("cdescr"));
c.setPid(rs.getInt("pid"));
c.setCno(rs.getInt("cno"));
c.setGrade(rs.getInt("grade"));
p.setCategory(c);
}
products.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rsCount);
DB.close(stmtCount);
DB.close(stmt);
DB.close(rs);
DB.close(conn);
}
return totalRecords;
}
public Product loadById(int id) {
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
Product p = null;
try {
String sql = "select * from product where id = " + id;
conn = DB.getConn();
stmt = DB.getStatement(conn);
rs = DB.getResultSet(stmt, sql);
if (rs.next()) {
p = getProductFromRs(rs);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(stmt);
DB.close(rs);
DB.close(conn);
}
return p;
}
public void update(Product p) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DB.getConn();
String sql = "update product set name=? , descr=?, normalprice=?, memberprice=?, categoryid=? where id=?";
pstmt = DB.prepare(conn, sql);
pstmt.setString(1, p.getName());
pstmt.setString(2, p.getDescr());
pstmt.setDouble(3, p.getNormalPrice());
pstmt.setDouble(4, p.getMemberPrice());
pstmt.setInt(5, p.getCategoryId());
pstmt.setInt(6, p.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(pstmt);
DB.close(conn);
}
}
private Product getProductFromRs(ResultSet rs) {
Product p = null;
try {
p = new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setDescr(rs.getString("descr"));
p.setNormalPrice(rs.getDouble("normalprice"));
p.setMemberPrice(rs.getDouble("memberprice"));
p.setPdate(rs.getTimestamp("pdate"));
p.setCategoryId(rs.getInt("categoryid"));
} catch (SQLException e) {
e.printStackTrace();
}
return p;
}
public void delete(String conditionStr) {
Connection conn = null;
Statement stmt = null;
String sql;
try {
conn = DB.getConn();
sql = "delete from product " + conditionStr;
stmt = DB.getStatement(conn);
DB.executeUpdate(stmt, sql);
} finally {
DB.close(stmt);
DB.close(conn);
}
}
public int find(List<Product> products, int pageNo, int pageSize, String queryStr) {
int totalRecords = -1;
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "";
sql = "select p.id productid, p.name pname, p.descr pdescr, p.normalprice, " +
" p.memberprice, p.pdate, p.categoryid , " +
" c.id categoryid, c.name cname, c.descr cdescr, c.pid, c.cno, c.grade " +
" from product p join category c on (p.categoryid = c.id)" + queryStr +
" order by p.pdate desc";
sql += " limit " + (pageNo - 1) * pageSize + "," + pageSize;
System.out.println(sql);
ResultSet rs = DB.getResultSet(stmt, sql);
Statement stmtCount = DB.getStatement(conn);
ResultSet rsCount = DB.getResultSet(stmtCount,
"select count(*) from product " + queryStr.replaceAll("p\\.", ""));
try {
rsCount.next();
totalRecords = rsCount.getInt(1);
while (rs.next()) {
Product p = null;
p = new Product();
p.setId(rs.getInt("productid"));
p.setName(rs.getString("pname"));
p.setDescr(rs.getString("pdescr"));
p.setNormalPrice(rs.getDouble("normalprice"));
p.setMemberPrice(rs.getDouble("memberprice"));
p.setPdate(rs.getTimestamp("pdate"));
p.setCategoryId(rs.getInt("categoryid"));
Category c = new Category();
c.setId(rs.getInt("categoryid"));
c.setName(rs.getString("cname"));
c.setDescr(rs.getString("cdescr"));
c.setPid(rs.getInt("pid"));
c.setCno(rs.getInt("cno"));
c.setGrade(rs.getInt("grade"));
p.setCategory(c);
products.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rsCount);
DB.close(stmtCount);
DB.close(stmt);
DB.close(rs);
DB.close(conn);
}
return totalRecords;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源概述: 此资源集包含了一套完整的Java实现的网上商城系统项目源代码、相关的毕业论文以及详尽的使用说明。它旨在提供一个全面、深入的学习和研究工具,适用于本科课程设计、毕业设计以及任何希望深入学习Java编程的学习者。 详细内容: 源代码——提供了一套完整、经过良好注释的Java源代码,实现了网上商城系统项目的全部功能。代码结构清晰,逻辑严谨,是学习Java编程和项目实践的优秀材料。 毕业论文——包含了一篇探讨网上商城系统项目背景、技术原理和实现过程的学术论文,可作为论文撰写参考。通过阅读论文,学习者可以了解项目的理论基础,深入理解项目的实现细节,提升分析和解决问题的能力。 使用说明——详细的使用说明文档帮助学习者快速了解项目结构和使用方法,降低学习难度,提高学习效率。 适用对象: 此资源集适用于计算机科学、软件工程等专业的本科生进行课程设计和毕业设计。同时,对于希望提升Java编程技能、了解项目实践过程的学习者,这也是一份不可多得的学习资料。
资源推荐
资源详情
资源评论
收起资源包目录
Java实现的网上商城系统源码,可用作毕业设计,课程设计 (722个子文件)
thcode.bmp 142B
OrderMySQLDAO.class 10KB
ProductMySQLDAO.class 9KB
User.class 8KB
CategoryService.class 7KB
ProductMgr.class 5KB
FileUpload.class 5KB
SalesCountServlet.class 4KB
DB.class 3KB
Category.class 3KB
TestContextParamServlet.class 2KB
BarChartDemo.class 2KB
OrderMgr.class 2KB
SalesOrder.class 2KB
AuthFilter.class 2KB
ProductSearchFormBean.class 2KB
StatService.class 2KB
Product.class 2KB
Cart.class 2KB
IPFilter.class 2KB
LoggedUserCounter.class 2KB
ProductOracleDAO.class 2KB
SalesItem.class 1KB
ProductStatItem.class 1018B
PropertyMgr.class 1007B
OrderDAO.class 864B
CartItem.class 816B
HelloWorldTag.class 750B
ProductDAO.class 689B
PasswordNotCorrectException.class 428B
UserNotFoundException.class 410B
GradeOutOfBoundsException.class 366B
.classpath 753B
new.css 4KB
style.css 2KB
toc.css 620B
newtitle.gif 202KB
pma_24.gif 60KB
pma_banner_12_1.gif 59KB
pma_4.gif 54KB
pma_20.gif 48KB
brand_banner.gif 47KB
pma_10.gif 37KB
pma_banner_21_1.gif 37KB
pma_banner_22_2.gif 36KB
bitmap.gif 34KB
index_01.gif 30KB
na-185.gif 21KB
tt0850.gif 13KB
pingpai_new.gif 11KB
242-98.gif 11KB
index.gif 11KB
fcthink2.gif 9KB
leftmf.gif 8KB
na-167.gif 8KB
paihang_top.gif 8KB
index_02.gif 8KB
op0141.gif 8KB
boy151.gif 8KB
tel.gif 7KB
redsign.gif 7KB
ex_dz.gif 7KB
ex_my.gif 6KB
br0197.gif 6KB
br0172.gif 6KB
br0402.gif 6KB
index_11.gif 6KB
ex_help.gif 6KB
br0373.gif 5KB
br0064.gif 5KB
op0129.gif 5KB
br0246.gif 5KB
op0155.gif 5KB
op0147.gif 5KB
op0174.gif 5KB
sv0054.gif 5KB
sv0055.gif 5KB
sv0056.gif 5KB
br0401.gif 5KB
br0355.gif 5KB
br0357.gif 5KB
br0467.gif 5KB
thinkpad_logo.gif 5KB
op0149.gif 5KB
tt0868.gif 5KB
op0156.gif 5KB
br0215.gif 5KB
bk0001.gif 5KB
br0352.gif 5KB
br0413.gif 5KB
tt0923.gif 5KB
tt0922.gif 5KB
br0109.gif 5KB
tt0919.gif 5KB
op0173.gif 5KB
br0233.gif 5KB
sv0051.gif 5KB
br0353.gif 5KB
br0294.gif 5KB
tt0910.gif 5KB
共 722 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
小新要变强
- 粉丝: 2w+
- 资源: 539
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功