步骤 2 : 基本的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.List;
import tmall.bean.Category;
import tmall.util.DBUtil;
public class CategoryDAO {
public int getTotal() {
int total = 0;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from Category";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public void add(Category bean) {
String sql = "insert into category values(null,?)";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, 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(Category bean) {
String sql = "update category set name= ? where id = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, bean.getName());
ps.setInt(2, 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 Category where id = " + id;
s.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Category get(int id) {
Category bean = null;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select * from Category where id = " + id;
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
bean = new Category();
String name = rs.getString(2);
bean.setName(name);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public List<Category> list() {
return list(0, Short.MAX_VALUE);
}
public List<Category> list(int start, int count) {
List<Category> beans = new ArrayList<Category>();
String sql = "select * from Category 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()) {
Category bean = new Category();
int id = rs.getInt(1);
String name = rs.getString(2);
bean.setId(id);
bean.setName(name);
beans.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return beans;
}
}
CategoryDAO 这个类比起后面的DAO比较单纯,基本上就是提供数据库相关的CRUD操作:
注: 这部分需要有JDBC基础才能学习,倘若JDBC基础比较薄弱,可以参考JDBC学习内容 1. 增加 public void add(Category bean) 2. 删除 public void delete(int id) 3. 修改 public void update(Category bean) 4. 根据id获取 public Category get(int id) 5. 分页查询 public List<Category> list(int start, int count) 6. 查询所有 public List<Category> list() 7. 获取总数 public int getTotal()
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|