StudentDao.java
上传用户:zaktkj
上传日期:2022-08-08
资源大小:5770k
文件大小:3k
源码类别:

JavaScript

开发平台:

JavaScript

  1. package com.family168.student;
  2. import java.sql.*;
  3. import java.util.*;
  4. public class StudentDao {
  5.     private static StudentDao instance = new StudentDao();
  6.     protected StudentDao() {
  7.     }
  8.     public static StudentDao getInstance() {
  9.         return instance;
  10.     }
  11.     public Page pagedQuery(int start, int limit, String sort, String dir) throws Exception {
  12.         String sql = "select limit " + start + " " + limit + " * from student";
  13.         if (sort != null && !sort.equals("") && dir != null && !dir.equals("")) {
  14.             sql += " order by " + sort + " " + dir;
  15.         }
  16.         Connection conn = DbUtils.getConn();
  17.         Statement state = conn.createStatement();
  18.         ResultSet rs = state.executeQuery(sql);
  19.         List result = new ArrayList();
  20.         while (rs.next()) {
  21.             Student student = new Student();
  22.             student.setId(rs.getLong("id"));
  23.             student.setCode(rs.getString("code"));
  24.             student.setName(rs.getString("name"));
  25.             student.setSex(rs.getInt("sex"));
  26.             student.setAge(rs.getInt("age"));
  27.             student.setPolitical(rs.getString("political"));
  28.             student.setOrigin(rs.getString("origin"));
  29.             student.setProfessional(rs.getString("professional"));
  30.             result.add(student);
  31.         }
  32.         rs = state.executeQuery("select count(*) from student");
  33.         int totalCount = 0;
  34.         if (rs.next()) {
  35.             totalCount = rs.getInt(1);
  36.         }
  37.         DbUtils.close(rs, state, conn);
  38.         Page page = new Page(totalCount, result);
  39.         return page;
  40.     }
  41.     public void insert(Student student) throws Exception {
  42.         String sql = "insert into student(code,name,sex,age,political,origin,professional) values(?,?,?,?,?,?,?)";
  43.         Connection conn = DbUtils.getConn();
  44.         PreparedStatement state = conn.prepareStatement(sql);
  45.         state.setString(1, student.getCode());
  46.         state.setString(2, student.getName());
  47.         state.setInt(3, student.getSex());
  48.         state.setInt(4, student.getAge());
  49.         state.setString(5, student.getPolitical());
  50.         state.setString(6, student.getOrigin());
  51.         state.setString(7, student.getProfessional());
  52.         state.executeUpdate();
  53.         DbUtils.close(null, state, conn);
  54.     }
  55.     public void update(Student student) throws Exception {
  56.         String sql = "update student set code=?,name=?,sex=?,age=?,political=?,origin=?,professional=? where id=?";
  57.         Connection conn = DbUtils.getConn();
  58.         PreparedStatement state = conn.prepareStatement(sql);
  59.         state.setString(1, student.getCode());
  60.         state.setString(2, student.getName());
  61.         state.setInt(3, student.getSex());
  62.         state.setInt(4, student.getAge());
  63.         state.setString(5, student.getPolitical());
  64.         state.setString(6, student.getOrigin());
  65.         state.setString(7, student.getProfessional());
  66.         state.setLong(8, student.getId());
  67.         state.executeUpdate();
  68.         DbUtils.close(null, state, conn);
  69.     }
  70.     public void remove(long id) throws Exception {
  71.         String sql = "delete from student where id=?";
  72.         Connection conn = DbUtils.getConn();
  73.         PreparedStatement state = conn.prepareStatement(sql);
  74.         state.setLong(1, id);
  75.         state.executeUpdate();
  76.         DbUtils.close(null, state, conn);
  77.     }
  78. }