步骤 2 : 基本的CRUD操作 步骤 3 : 非CRUD方法
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package tmall.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import tmall.bean.Product;
import tmall.bean.Review;
import tmall.bean.User;
import tmall.util.DBUtil;
import tmall.util.DateUtil;
public class ReviewDAO {
public int getTotal() {
int total = 0;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from Review";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public int getTotal(int pid) {
int total = 0;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from Review where pid = " + pid;
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public void add(Review bean) {
String sql = "insert into Review values(null,?,?,?,?)";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, bean.getContent());
ps.setInt(2, bean.getUser().getId());
ps.setInt(3, bean.getProduct().getId());
ps.setTimestamp(4, DateUtil.d2t(bean.getCreateDate()));
ps.execute();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(Review bean) {
String sql = "update Review set content= ?, uid=?, pid=? , createDate = ? where id = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, bean.getContent());
ps.setInt(2, bean.getUser().getId());
ps.setInt(3, bean.getProduct().getId());
ps.setTimestamp(4, DateUtil.d2t( bean.getCreateDate()) );
ps.setInt(5, bean.getId());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "delete from Review where id = " + id;
s.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Review get(int id) {
Review bean = new Review();
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select * from Review where id = " + id;
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
int pid = rs.getInt("pid");
int uid = rs.getInt("uid");
Date createDate = DateUtil.t2d(rs.getTimestamp("createDate"));
String content = rs.getString("content");
Product product = new ProductDAO().get(pid);
User user = new UserDAO().get(uid);
bean.setContent(content);
bean.setCreateDate(createDate);
bean.setProduct(product);
bean.setUser(user);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public List<Review> list(int pid) {
return list(pid, 0, Short.MAX_VALUE);
}
public int getCount(int pid) {
String sql = "select count(*) from Review where pid = ? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, pid);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public List<Review> list(int pid, int start, int count) {
List<Review> beans = new ArrayList<Review>();
String sql = "select * from Review where pid = ? order by id desc limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, pid);
ps.setInt(2, start);
ps.setInt(3, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Review bean = new Review();
int id = rs.getInt(1);
int uid = rs.getInt("uid");
Date createDate = DateUtil.t2d(rs.getTimestamp("createDate"));
String content = rs.getString("content");
Product product = new ProductDAO().get(pid);
User user = new UserDAO().get(uid);
bean.setContent(content);
bean.setCreateDate(createDate);
bean.setId(id);
bean.setProduct(product);
bean.setUser(user);
beans.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return beans;
}
public boolean isExist(String content, int pid) {
String sql = "select * from Review where content = ? and pid = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, content);
ps.setInt(2, pid);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2021-11-30
ReviewDAO的isExist(String content, int pid)方法是否多余
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2021-02-20
SQL语句中的表名和数据库里的表名好像不一样啊
2020-06-12
getCount方法中,为什么是while(rs.next())而不是if(rs.next())
2020-04-28
站长,这里的getCount(int pid) 和getTotal(int pid)有什么不同吗?不都是返回某个产品的评论数目吗?为啥要有两个方法啊
2019-05-13
每一个DAO类都要重复写一次CRUD的不会显得很繁琐吗
提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 10 条以前的提问,请 点击查看
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|