// 查询所有学生信息 ,以注册时间为顺序 public List<Student> selectAllSt(Page page) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<Student> list = null; String sql = "select * from student order by register_date desc limit ?,?"; try { conn = JdbcUtil.getConnection(); list = new ArrayList<Student>(); st = conn.prepareStatement(sql); // 预编译SQL语句 st.setInt(1, page.getStart()); st.setInt(2, page.getShowCount()); rs = st.executeQuery(); while (rs.next()) { // 逐行查询 Student student = new Student(); student.setId(rs.getInt("id")); student.setUsername(rs.getString("username")); student.setSex(rs.getString("sex")); student.setEmail(rs.getString("email")); student.setTel(rs.getString("tel")); student.setName(rs.getString("name")); student.setRegister_date(rs.getDate("register_date")); list.add(student); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.release(conn, st, rs); } return list; }
// 查询一个学生的所有信息 public Student selectSt(String username) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; Student student = null; String sql = "select * from student where username=? "; try { student = new Student(); conn = JdbcUtil.getConnection(); st = conn.prepareStatement(sql); st.setString(1, username); rs = st.executeQuery(); while (rs.next()) { if (username.equals(rs.getString("username"))) { student.setId(rs.getInt("id")); student.setUsername(rs.getString("username")); student.setEmail(rs.getString("email")); student.setName(rs.getString("name")); student.setPhoto(rs.getString("photo")); student.setAnswer(rs.getString("answer")); student.setProtection(rs.getString("protection")); student.setTel(rs.getString("tel")); student.setView_photo(rs.getString("view_photo")); } } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.release(conn, st, rs); } return student; }