how2j.cn

步骤 1 : ReviewDAO   
步骤 2 : 基本的CRUD操作   
步骤 3 : 非CRUD方法   

ReviewDAO用于建立对于Review对象的ORM映射
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; } }
步骤 2 :

基本的CRUD操作

edit
基本的CRUD操作与ReviewDAO的CRUD一样,在此不做赘述
除开CRUD之外,ReviewDAO还提供了一些其他用于支持业务的方法

获取指定产品一共有多少条评价

public int getCount(int pid)


获取指定产品的评价

public List<Review> list(int pid)
public List<Review> list(int pid, int start, int count)


注:部分非CRUD的业务方法,需要结合业务场景才能更好地理解,现在理解不透彻也很正常,在后续学习到相关场景的时候,再回过头来看,就明白了。


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 回收站-删除掉的-ReviewDAO 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图