StudentSystemHtml.rar

  • 明流祸妖
    了解作者
  • Java
    开发工具
  • 2.8MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2020-12-06 19:17
    上传日期
简易数据库查询系统,使用了jsp与servlet进行前后端串联
StudentSystemHtml.rar
内容介绍
package studentSystem; import java.sql.*; import java.util.ArrayList; import java.util.List; //用于进行数据操作的类 public class StudentDAO { public boolean login(String user,String password) { String sql = "SELECT COUNT(*) FROM admin where user = ? and password = ?"; try { Connection c = DBC.getConnection(); PreparedStatement ps = c.prepareStatement(sql); ps.setString(1, user); ps.setString(2, password); ResultSet rs = ps.executeQuery(); while (rs.next()) { if(rs.getInt(1)==1) return true; else return false; } } catch (SQLException e) { e.printStackTrace(); } return false; } public int getTotal() throws ClassNotFoundException { int total = 0; String sql = "SELECT COUNT(*) FROM student"; try { Connection c = DBC.getConnection(); Statement st = c.createStatement(); ResultSet rs = st.executeQuery(sql); while (rs.next()) { total = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return total; } public void add(Student student) throws ClassNotFoundException { String sql = "INSERT INTO student VALUES(?,?,?,?,?,?)"; try{ Connection c = DBC.getConnection(); PreparedStatement ps = c.prepareStatement(sql); ps.setString(1, student.getSno()); ps.setString(2, student.getName()); ps.setInt(3, student.getAge()); ps.setString(4, student.getSex()); ps.setDate(5, new java.sql.Date(student.getBirthday().getTime())); ps.setString(6, student.getGrade()); ps.execute(); } catch (SQLException e) { e.printStackTrace(); } } public void delete(String sno) throws ClassNotFoundException { String sql = "DELETE FROM student WHERE sno = ?"; try { Connection c = DBC.getConnection(); PreparedStatement ps = c.prepareStatement(sql); ps.setString(1, sno); ps.execute(); } catch (SQLException e) { e.printStackTrace(); } } public void update(Student student) throws ClassNotFoundException { String sql = "update student set name = ?, age = ?, sex = ?, birthday = ? ,grade = ? where sno = ? "; try { Connection c = DBC.getConnection(); PreparedStatement ps = c.prepareStatement(sql); ps.setString(1, student.getName()); ps.setInt(2, student.getAge()); ps.setString(3, student.getSex()); ps.setDate(4, new java.sql.Date(student.getBirthday().getTime())); ps.setString(5, student.getGrade()); ps.setString(6, student.getSno()); ps.execute(); } catch (SQLException e) { e.printStackTrace(); } } public Student get(String sno) throws ClassNotFoundException { Student student = new Student(); String sql = "SELECT * FROM student WHERE sno = " + sno; try { Connection c = DBC.getConnection(); Statement st = c.createStatement(); ResultSet rs = st.executeQuery(sql); if (rs.next()) { String name = rs.getString("name"); int age = rs.getInt("age"); String sex = rs.getString("sex"); Date birthday = rs.getDate("birthday"); String grade=rs.getString("grade"); student.setSno(sno); student.setName(name); student.setAge(age); student.setSex(sex); student.setBirthday(birthday); student.setGrade(grade); } } catch (SQLException e) { e.printStackTrace(); } return student; } public List<Student> list() throws ClassNotFoundException { return getList(); } public List<Student> getList() throws ClassNotFoundException { List<Student> students = new ArrayList<>(); String sql = "SELECT * FROM student"; try { Connection c = DBC.getConnection(); PreparedStatement ps = c.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { Student student = new Student(); String sno = rs.getString("sno"); String name = rs.getString("name"); int age = rs.getInt("age"); String sex = rs.getString("sex"); Date birthday = rs.getDate("birthday"); String grade=rs.getString("grade"); student.setSno(sno); student.setName(name); student.setAge(age); student.setSex(sex); student.setBirthday(birthday); student.setGrade(grade); students.add(student); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } return students; } }
评论
    相关推荐