how2j.cn

步骤 1 : PropertyValueDAO.java   
步骤 2 : 基本的CRUD操作   
步骤 3 : 非CRUD方法   

步骤 1 :

PropertyValueDAO.java

edit edit
增值内容,请先登录
完整的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.List; import tmall.bean.Product; import tmall.bean.Property; import tmall.bean.PropertyValue; import tmall.util.DBUtil; public class PropertyValueDAO { public int getTotal() { int total = 0; try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) { String sql = "select count(*) from PropertyValue"; ResultSet rs = s.executeQuery(sql); while (rs.next()) { total = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return total; } public void add(PropertyValue bean) { String sql = "insert into PropertyValue values(null,?,?,?)"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, bean.getProduct().getId()); ps.setInt(2, bean.getProperty().getId()); ps.setString(3, bean.getValue()); 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(PropertyValue bean) { String sql = "update PropertyValue set pid= ?, ptid=?, value=? where id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, bean.getProduct().getId()); ps.setInt(2, bean.getProperty().getId()); ps.setString(3, bean.getValue()); ps.setInt(4, 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 PropertyValue where id = " + id; s.execute(sql); } catch (SQLException e) { e.printStackTrace(); } } public PropertyValue get(int id) { PropertyValue bean = new PropertyValue(); try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) { String sql = "select * from PropertyValue where id = " + id; ResultSet rs = s.executeQuery(sql); if (rs.next()) { int pid = rs.getInt("pid"); int ptid = rs.getInt("ptid"); String value = rs.getString("value"); Product product = new ProductDAO().get(pid); Property property = new PropertyDAO().get(ptid); bean.setProduct(product); bean.setProperty(property); bean.setValue(value); bean.setId(id); } } catch (SQLException e) { e.printStackTrace(); } return bean; } public PropertyValue get(int ptid, int pid ) { PropertyValue bean = null; try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) { String sql = "select * from PropertyValue where ptid = " + ptid + " and pid = " + pid; ResultSet rs = s.executeQuery(sql); if (rs.next()) { bean= new PropertyValue(); int id = rs.getInt("id"); String value = rs.getString("value"); Product product = new ProductDAO().get(pid); Property property = new PropertyDAO().get(ptid); bean.setProduct(product); bean.setProperty(property); bean.setValue(value); bean.setId(id); } } catch (SQLException e) { e.printStackTrace(); } return bean; } public List<PropertyValue> list() { return list(0, Short.MAX_VALUE); } public List<PropertyValue> list(int start, int count) { List<PropertyValue> beans = new ArrayList<PropertyValue>(); String sql = "select * from PropertyValue order by id desc limit ?,? "; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, start); ps.setInt(2, count); ResultSet rs = ps.executeQuery(); while (rs.next()) { PropertyValue bean = new PropertyValue(); int id = rs.getInt(1); int pid = rs.getInt("pid"); int ptid = rs.getInt("ptid"); String value = rs.getString("value"); Product product = new ProductDAO().get(pid); Property property = new PropertyDAO().get(ptid); bean.setProduct(product); bean.setProperty(property); bean.setValue(value); bean.setId(id); beans.add(bean); } } catch (SQLException e) { e.printStackTrace(); } return beans; } public void init(Product p) { List<Property> pts= new PropertyDAO().list(p.getCategory().getId()); for (Property pt: pts) { PropertyValue pv = get(pt.getId(),p.getId()); if(null==pv){ pv = new PropertyValue(); pv.setProduct(p); pv.setProperty(pt); this.add(pv); } } } public List<PropertyValue> list(int pid) { List<PropertyValue> beans = new ArrayList<PropertyValue>(); String sql = "select * from PropertyValue where pid = ? order by ptid desc"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, pid); ResultSet rs = ps.executeQuery(); while (rs.next()) { PropertyValue bean = new PropertyValue(); int id = rs.getInt(1); int ptid = rs.getInt("ptid"); String value = rs.getString("value"); Product product = new ProductDAO().get(pid); Property property = new PropertyDAO().get(ptid); bean.setProduct(product); bean.setProperty(property); bean.setValue(value); bean.setId(id); beans.add(bean); } } catch (SQLException e) { e.printStackTrace(); } return beans; } }
步骤 2 :

基本的CRUD操作

edit edit
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢


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


提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 实践项目-天猫整站J2EE-PropertyValueDAO 的提问

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

上传截图