该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
接下来写dao函数类:
package dao;import dbutil.Dbconn;
import entity.Student;
import entity.Teacher;import java.sql.*;
import java.util.ArrayList;
import java.util.List;public class TeacherDao {
private Statement st;
private ResultSet rs;
private Connection conn;
private Dbconn s;
//预编译语句
private PreparedStatement ps;
public TeacherDao(){
s=new Dbconn();
conn=s.getConnection();
}
public List list(){
List teachers=new ArrayList<>();
try{
String sql="select * from teacher";
st=conn.createStatement();
rs=st.executeQuery(sql);
Teacher teacher;
while(rs.next()){
teacher=new Teacher();
teacher.setId(rs.getInt("id"));
teacher.setSex(rs.getString("sex"));
teacher.setBirthday(rs.getString("birthday"));
teacher.setWork(rs.getString("work"));
teacher.setMoblenum(rs.getInt("moblenum"));
teacher.setName(rs.getString("name"));
teachers.add(teacher);
}
// s.closeAll(conn,st,rs);
}
catch (Exception e){
e.printStackTrace();
}
return teachers;
}
public Teacher load(int id){
Teacher teacher=null;
try {
String sql="select * from teacher where id=?";
ps=conn.prepareStatement(sql);
ps.setInt(1,id);
rs=ps.executeQuery();
if (rs.next())
{
teacher=new Teacher();
teacher.setId(rs.getInt("id"));
teacher.setSex(rs.getString("sex"));
teacher.setBirthday(rs.getString("birthday"));
teacher.setWork(rs.getString("work"));
teacher.setMoblenum(rs.getInt("moblenum"));
teacher.setName(rs.getString("name")); }
} catch (SQLException e) {
e.printStackTrace();
}
return teacher;
} public List search(String searchName){
List teachers=new ArrayList<>();
try{
String sql="select * from teacher where name like ?";
ps=conn.prepareStatement(sql);
ps.setString(1,"%"+searchName+"%");
rs=ps.executeQuery();
Teacher teacher;
while(rs.next()){
teacher=new Teacher();
teacher.setId(rs.getInt("id"));
teacher.setSex(rs.getString("sex"));
teacher.setBirthday(rs.getString("birthday"));
teacher.setWork(rs.getString("work"));
teacher.setMoblenum(rs.getInt("moblenum"));
teacher.setName(rs.getString("name"));
teachers.add(teacher);
}
// s.closeAll(conn,st,rs);
}
catch (Exception e){
e.printStackTrace();
}
return teachers;
} public void delete(int id){
try {
String sql="delete from teacher where id=? ";
ps=conn.prepareStatement(sql);
ps.setInt(1,id);
ps.execute(); } catch (SQLException e) {
e.printStackTrace();
}
} public void update(Teacher teacher){
try {
String sql="update teacher set sex=?,birthday=?,work=?,moblenum=?,name=? where id=?";
ps=conn.prepareStatement(sql);
ps.setString(1,teacher.getSex());
ps.setString(2,teacher.getBirthday());
ps.setString(3,teacher.getWork());
ps.setInt(4,teacher.getMoblenum());
ps.setString(5,teacher.getName());
ps.setInt(6,teacher.getId());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
} public void insert(Teacher teacher){
try {
String sql="insert into teacher(sex,birthday,work,moblenum,name) values(?,?,?,?,?)";
ps=conn.prepareStatement(sql);
// ps.setInt(1,teacher.getId());
ps.setString(1,teacher.getSex());
ps.setString(2,teacher.getBirthday());
ps.setString(3,teacher.getWork());
ps.setInt(4,teacher.getMoblenum());
ps.setString(5,teacher.getName());
ps.execute();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
}