コード例 #1
0
  @Override
  public List<Employee> getAllEmployees() throws SQLException {
    // TODO Auto-generated method stub
    List<Employee> empList = new ArrayList<Employee>();
    String sql = "Select * from employees;";
    DbConnection db = new DbConnection();
    Connection con = db.getConnection();
    PreparedStatement ps = con.prepareStatement(sql);

    ResultSet r = ps.executeQuery();

    if (r != null) {
      while (r.next()) {
        Employee e = new Employee();
        e.setId(r.getInt("id"));
        e.setFull_name(r.getString("full_name"));
        e.setGender(r.getString("gender"));
        e.setPhone(r.getString("phone"));
        e.setAddress(r.getString("address"));
        e.setUser_id(r.getInt("dept_id"));
        empList.add(e);
      }
      r.close();
      ps.close();
      con.close();
    }
    return empList;
  }
コード例 #2
0
  @Override
  public Employee selectEmployee(Integer id) throws SQLException {
    // TODO Auto-generated method stub
    String sql = "Select * from employees where (id = ? );";
    DbConnection db = new DbConnection();
    // Database db = new Database();
    // db.setDefaultParameters();
    // db.setConnection(true);
    Connection con = null;
    if (db == null) {
      System.out.println("Null database");
      System.exit(1);
    }
    con = db.getConnection();
    if (con == null) {
      System.out.println("Null connection");
      System.exit(2);
    }
    PreparedStatement ps = null;
    ps = con.prepareStatement(sql);
    ps.setInt(1, id);

    ResultSet r = ps.executeQuery();
    Employee e = new Employee();
    if (r != null) {
      if (r.next()) {
        e.setId(r.getInt("id"));
        e.setFull_name(r.getString("full_name"));
        e.setGender(r.getString("gender"));
        e.setPhone(r.getString("phone"));
        e.setAddress(r.getString("address"));
        e.setUser_id(r.getInt("dept_id"));
      }
      r.close();
      ps.close();
      con.close();
    }
    return e;
    // return null;
  }