步骤 2 : 基本的CRUD 步骤 3 : 非CRUD方法 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.Category;
import tmall.bean.Product;
import tmall.bean.Property;
import tmall.util.DBUtil;
import tmall.util.DateUtil;
public class PropertyDAO {
public int getTotal(int cid) {
int total = 0;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from Property where cid =" + cid;
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public void add(Property bean) {
String sql = "insert into Property values(null,?,?)";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, bean.getCategory().getId());
ps.setString(2, bean.getName());
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(Property bean) {
String sql = "update Property set cid= ?, name=? where id = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, bean.getCategory().getId());
ps.setString(2, bean.getName());
ps.setInt(3, 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 Property where id = " + id;
s.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Property get(String name, int cid) {
Property bean =null;
String sql = "select * from Property where name = ? and cid = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, name);
ps.setInt(2, cid);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
bean = new Property();
bean.setName(name);
Category category = new CategoryDAO().get(cid);
bean.setCategory(category);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public Property get(int id) {
Property bean = new Property();
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select * from Property where id = " + id;
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
String name = rs.getString("name");
int cid = rs.getInt("cid");
bean.setName(name);
Category category = new CategoryDAO().get(cid);
bean.setCategory(category);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public List<Property> list(int cid) {
return list(cid, 0, Short.MAX_VALUE);
}
public List<Property> list(int cid, int start, int count) {
List<Property> beans = new ArrayList<Property>();
String sql = "select * from Property where cid = ? order by id desc limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, cid);
ps.setInt(2, start);
ps.setInt(3, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Property bean = new Property();
int id = rs.getInt(1);
String name = rs.getString("name");
bean.setName(name);
Category category = new CategoryDAO().get(cid);
bean.setCategory(category);
bean.setId(id);
beans.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return beans;
}
}
基本的CRUD操作与CategoryDAO的CRUD一样,在此不做赘述
除开CRUD之外,PropertyDAO还提供了一些其他用于支持业务的方法。
获取某种分类下的属性总数,在分页显示的时候会用到 public int getTotal(int cid) 查询某个分类下的的属性对象 public List<Property> list(int cid, int start, int count) public List<Property> list(int cid) 注:部分非CRUD的业务方法,需要结合业务场景才能更好地理解,现在理解不透彻也很正常,在后续学习到相关场景的时候,再回过头来看,就明白了。
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|