1 //下面的一些关于DBUtil的内容,可以去查看前面的一篇DBUtil类
  2 
  3 @Override
  4 //这里使用DBUtil中的封装方法,代码清晰更简单
  5 //添加之前也应该查询一下数据库内是否已有该条数据
  6 public boolean add(Manager manager) {
  7     boolean flag = false;
  8     Manager manager1 = findById(manager.getMid());
  9     if (manager1 != null) {
 10         flag = false;
 11         System.out.println("添加失败,数据库中已有" + manager.getMid() + "编号的数据");
 12     } else {
 13         String sql = "insert into manager (mid, mname, mpassword, tel) values (?, ?, ?, ?)";
 14         Object[] objs = {manager.getMid(), manager.getMname(), manager.getMpassword(), manager.getTel()};
 15         int n = DBUtil.executeDML(sql, objs);
 16         if(n > 0) {
 17             flag = true;
 18         } else {
 19             flag = false;
 20         }
 21     }
 22     return flag;
 23 }
 24 
 25 @Override
 26 public Manager findById(int id) {
 27     Manager manager = null;
 28     Connection conn = DBUtil.getConnection();
 29     String sql = "select * from manager where mid = ?";
 30     PreparedStatement pstmt = DBUtil.getPreparedStatement(conn, sql);
 31     ResultSet rs = null;
 32     try {
 33         pstmt.setInt(1, id);
 34         rs = pstmt.executeQuery();
 35         if (rs.next()) {
 36             manager = new Manager(rs.getInt("mid"), rs.getString("mname"),
 37                     rs.getString("mpassword"), rs.getString("tel"));
 38         }
 39     } catch (Exception e) {
 40         e.printStackTrace();
 41     } finally {
 42         DBUtil.closeAll(rs, pstmt, conn);
 43     }
 44     return manager;
 45 }
 46 
 47 @Override
 48 //这里使用了DBUtil中写的那个封装更新语句,更方便,代码更清晰
 49 public boolean update(Manager manager) {
 50     boolean flag = false;
 51     String sql = "update manager set mname = ?, mpassword = ?, tel = ? where mid = ?";
 52     Object[] objs = {manager.getMname(), manager.getMpassword(), manager.getTel(), manager.getMid()};
 53     if (DBUtil.executeDML(sql, objs) > 0) {
 54         flag = true;
 55     }
 56     return flag;
 57 }
 58 
 59 @Override
 60 public boolean delete(int id) {
 61     Connection conn = DBUtil.getConnection();
 62     String sql = "DELETE FROM manager WHERE mid = ?";
 63     PreparedStatement pstmt = DBUtil.getPreparedStatement(conn, sql);
 64     boolean flag = false;
 65     try {
 66         pstmt.setInt(1, id);
 67         int n = pstmt.executeUpdate();
 68         if (n > 0) {
 69             flag = true;
 70         }
 71     } catch (Exception e) {
 72         e.printStackTrace();
 73     } finally {
 74         DBUtil.closeAll(null, pstmt, conn);
 75     }
 76     return flag;
 77 }
 78 
 79 @Override
 80 //使用集合封装所有查询的数据
 81 public List<Manager> findAll() {
 82     Manager manager = null;
 83     List<Manager> list = new ArrayList<Manager>();
 84     Connection conn = DBUtil.getConnection();
 85     String sql = "select * from manager";
 86     PreparedStatement pstmt = DBUtil.getPreparedStatement(conn, sql);
 87     ResultSet rs = null;
 88     try {
 89         rs = pstmt.executeQuery();
 90         while (rs.next()) {
 91             manager = new Manager(rs.getInt("mid"), rs.getString("mname"),
 92                     rs.getString("mpassword"), rs.getString("tel"));
 93             list.add(manager);
 94         }
 95     } catch (Exception e) {
 96         e.printStackTrace();
 97     } finally {
 98         DBUtil.closeAll(rs, pstmt, conn);
 99     }
100     return list;
101 }