// 4.保存查询出来的数据到list
List list = new ArrayList<>();
while (rs.next()) {
Teacher teacher = new Teacher();
teacher.setId(rs.getInt(“id”));
teacher.setName(rs.getString(“name”));
teacher.setSex(rs.getString(“sex”));
teacher.setSubject(rs.getString(“subject”));
teacher.setSalary(rs.getDouble(“salary”));
teacher.setWorkTime(rs.getString(“workTime”));
list.add(teacher);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭链接,避免数据库连接过多
DBUtil.close(con, ps, rs);
}
return null;
}
/**
-
根据教职工号查询教师信息
-
@param id
-
@return
*/
@Override
public Teacher selectTeacher(Integer id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1.连接数据库
con = DBUtil.getConnection();
// 2.预编译
String sql = “select * from Teacher where id = ?”;
ps = con.prepareStatement(sql);
ps.setInt(1,id);
// 3.执行sql
rs = ps.executeQuery();
while (rs.next()){
Teacher teacher = new Teacher();
teacher.setId(rs.getInt(“id”));
teacher.setName(rs.getString(“name”));
teacher.setSex(rs.getString(“sex”));
teacher.setSubject(rs.getString(“subject”));
teacher.setSalary(rs.getDouble(“salary”));
teacher.setWorkTime(rs.getString(“workTime”));
return teacher;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
// 关闭资源,避免出现异常
DBUtil.close(con,ps,rs);
}
return null;
}
/**
-
新增教师信息
-
@param teacher
-
@return
*/
@Override
public boolean addTeacher(Teacher teacher) {
String sql = “insert into teacher values (?,?,?,?,?,?)”;
List list = new ArrayList();
list.add(teacher.getId());
list.add(teacher.getName());
list.add(teacher.getSex());
list.add(teacher.getSubject());
list.add(teacher.getSalary());
list.add(teacher.getWorkTime());
boolean flag = DBUtil.addUpdateDelete(sql,list.toArray());
if(flag){
return true;
}else {
return false;
}
}
/**
-
更新教师信息
-
@param teacher
-
@return
*/
@Override
public boolean updateTeacher(Teacher teacher) {
String sql = “update teacher set name=?,sex=?,subject=?,salary=?,workTime=? where id=?”;
List list = new ArrayList();
list.add(teacher.getName());<