简单的Dao层的模板写法
Posted huangpeideng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的Dao层的模板写法相关的知识,希望对你有一定的参考价值。
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;
以上是关于简单的Dao层的模板写法的主要内容,如果未能解决你的问题,请参考以下文章