1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package dao;
import model.Student;
import java.sql.*; import java.text.SimpleDateFormat; import java.util.Vector;
public class StudentDao extends Util{
public static Vector<Student> getAllStudent() throws ClassNotFoundException, SQLException { Vector<Student> students=new Vector<>(); Connection connection=getConnection(); Statement statement=connection.createStatement(); ResultSet resultSet=statement.executeQuery("select * from stu"); while(resultSet.next()) { Student student=new Student(); student.setStuNo(resultSet.getString(1)); student.setName(resultSet.getString(2)); student.setSex(resultSet.getString(3)); student.setBirthday(resultSet.getString(4)); student.setPro(resultSet.getString(5)); student.setSalary(resultSet.getInt(6)); students.add(student); } closeAll(connection, statement,resultSet); return students; }
public static Student getOneStudent(String id) throws ClassNotFoundException, SQLException { Student student=null; Connection connection=getConnection(); PreparedStatement statement=connection.prepareStatement("select * from stu where stuNo=?"); statement.setString(1, id); ResultSet resultSet=statement.executeQuery(); while(resultSet.next()) { student=new Student(); student.setStuNo(resultSet.getString(1)); student.setName(resultSet.getString(2)); student.setSex(resultSet.getString(3)); student.setBirthday(resultSet.getString(4)); student.setPro(resultSet.getString(5)); student.setSalary(resultSet.getInt(6)); } closeAll(connection, statement,resultSet); return student; }
public static void addOneStudent(Student student) throws ClassNotFoundException, SQLException { Connection connection=getConnection(); PreparedStatement statement=connection.prepareStatement( "insert into stu values(?,?,?,?,?,?)"); statement.setString(1, student.getStuNo()); statement.setString(2, student.getName()); statement.setString(3, student.getSex()); statement.setString(4, student.getBirthday()); statement.setString(5, student.getPro()); statement.setInt(6, student.getSalary()); statement.executeUpdate(); closeAll(connection, statement); }
public static void delOneStudent(String id) throws ClassNotFoundException, SQLException { Connection connection=getConnection(); PreparedStatement statement=connection.prepareStatement( "delete from stu where stuNo=?"); statement.setString(1, id); statement.executeUpdate(); closeAll(connection, statement); }
public static void updateOneStudent(Student student) throws ClassNotFoundException, SQLException { Connection connection=getConnection(); PreparedStatement statement=connection.prepareStatement( "update stu set name=?,sex=?,birthday=?,pro=?,salary=?" + " where stuNo=?"); statement.setString(1, student.getName()); statement.setString(2, student.getSex()); statement.setString(3, student.getBirthday()); statement.setString(4, student.getPro()); statement.setInt(5, student.getSalary()); statement.setString(6, student.getStuNo()); statement.executeUpdate(); closeAll(connection, statement); }
}
|