// 分页查询所有状态 public List<Teacher> selectAllAds(Page page) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<Teacher> list = null; String sql = "select * from ads order by ads_date desc limit ?,?"; try { conn = JdbcUtil.getConnection(); list = new ArrayList<Teacher>(); st = conn.prepareStatement(sql); st.setInt(1, page.getStart()); st.setInt(2, page.getShowCount()); rs = st.executeQuery(); while (rs.next()) { Teacher teacher = new Teacher(); teacher.setId(rs.getInt("id")); teacher.setUsername(rs.getString("thusername")); teacher.setContent(rs.getString("content")); teacher.setAds_date(Date.valueOf(rs.getString("ads_date"))); list.add(teacher); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.release(conn, st, rs); } return list; }
// 更新一个老师数据(后台) public boolean updateTh(Teacher teacher) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; String sql = "update teacher set name=?,tel=?,bank=?,banknum=?,score=?,introduction=? where id=?"; try { conn = JdbcUtil.getConnection(); st = conn.prepareStatement(sql); st.setString(1, teacher.getName()); st.setString(2, teacher.getTel()); st.setString(3, teacher.getBank()); st.setString(4, teacher.getBanknum()); st.setInt(5, teacher.getScore()); st.setString(6, teacher.getIntroduction()); st.setInt(7, teacher.getId()); st.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); return false; } finally { JdbcUtil.release(conn, st, rs); } return true; }
// 根据username查询某个老师发布的信息 public List<Teacher> selectAds(Teacher teacher) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<Teacher> list = null; String sql = "select * from ads where thusername=? order by ads_date desc limit 0,4"; try { list = new ArrayList<Teacher>(); conn = JdbcUtil.getConnection(); st = conn.prepareStatement(sql); st.setString(1, teacher.getUsername()); rs = st.executeQuery(); while (rs.next()) { Teacher _teacher = new Teacher(); _teacher.setUsername(teacher.getUsername()); _teacher.setContent(rs.getString("content")); _teacher.setAds_date(rs.getDate("ads_date")); list.add(_teacher); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.release(conn, st, rs); } return list; }
// 分页展示老师信息 public List<Teacher> selectAllTh(Page page) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<Teacher> list = null; String sql = "select * from teacher order by score desc limit ?,?"; try { conn = JdbcUtil.getConnection(); list = new ArrayList<Teacher>(); st = conn.prepareStatement(sql); // 预编译SQL语句 st.setInt(1, page.getStart()); st.setInt(2, page.getShowCount()); rs = st.executeQuery(); while (rs.next()) { // 逐行查询 Teacher teacher = new Teacher(); teacher.setId(rs.getInt("id")); teacher.setName(rs.getString("name")); teacher.setPhoto(rs.getString("photo")); teacher.setCollege(rs.getString("college")); teacher.setUsername(rs.getString("username")); teacher.setSex(rs.getString("sex")); teacher.setLanguage(rs.getString("language")); teacher.setEducation(rs.getString("education")); teacher.setEmail(rs.getString("email")); teacher.setTel(rs.getString("tel")); teacher.setScore(rs.getInt("score")); teacher.setBank(rs.getString("bank")); teacher.setBanknum(rs.getString("banknum")); list.add(teacher); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.release(conn, st, rs); } return list; }
// 查询一个分数最高的老师 public Teacher selectThFirst() { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; Teacher teacher = null; String sql = "select * from teacher order by score desc limit 0,1"; try { teacher = new Teacher(); conn = JdbcUtil.getConnection(); st = conn.prepareStatement(sql); rs = st.executeQuery(); while (rs.next()) { teacher.setId(rs.getInt("id")); teacher.setName(rs.getString("name")); teacher.setSex(rs.getString("sex")); teacher.setTel(rs.getString("tel")); teacher.setScore(rs.getInt("score")); teacher.setBank(rs.getString("bank")); teacher.setPhoto(rs.getString("photo")); teacher.setAnswer(rs.getString("answer")); teacher.setEmail(rs.getString("email")); teacher.setBanknum(rs.getString("banknum")); teacher.setLanguage(rs.getString("language")); teacher.setBirthday(rs.getDate("birthday")); teacher.setCollege(rs.getString("college")); teacher.setUsername(rs.getString("username")); teacher.setView_photo(rs.getString("view_photo")); teacher.setPrice(rs.getString("price")); teacher.setEducation(rs.getString("education")); teacher.setProtection(rs.getString("protection")); teacher.setIntroduction(rs.getString("introduction")); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtil.release(conn, st, rs); } return teacher; }