1. 程式人生 > >dao層開發程式碼

dao層開發程式碼

StudentDAO介面,定義學生相關的操作


/*
 * StudentDAO介面,定義學生相關的操作
 */
public interface StudentDAO {

    //新增學生
    public void addStudent(Student stu);

    //刪除學生
    public void deleteStudent(int id);

    //修改學生
    public void updateStudent(Student stu);

    //查詢所有學生
    public List<Student> getAllStudents();

    //根據學號查詢學生
public Student getStudentById(int id); //根據條件模糊查詢 public List<Student> getStudentsByCondition(String name,String gender); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

StudentDAOImpl實現類,實現相關的操作

/*
 * StudentDAOImpl實現類,實現相關的操作
 */
public class StudentDAOImpl implements StudentDAO {
    Connection conn=null;
PreparedStatement pstmt=null; ResultSet rs=null; @Override public void addStudent(Student stu) { String sql="insert into student values (null,?,?,?,?,?)"; try { conn=DBUtil.getConnection(); pstmt=conn.prepareStatement(sql); pstmt.setString
(1, stu.getName()); pstmt.setInt(2, stu.getAge()); pstmt.setString(3, stu.getGender()); pstmt.setString(4, stu.getPhone()); pstmt.setString(5, stu.getEducation()); pstmt.executeUpdate(); System.out.println("新增學生成功!"); } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.closeAll(rs, pstmt, conn); } } @Override public void deleteStudent(int id) { String sql="delete from student where id=?"; try { conn=DBUtil.getConnection(); pstmt=conn.prepareStatement(sql); pstmt.setObject(1, id); pstmt.executeUpdate(); System.out.println("刪除學生成功!"); } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.closeAll(rs, pstmt, conn); } } @Override public void updateStudent(Student stu) { String sql="update student set name=?,age=?,gender=?,phone=?,education=? where id=?"; try { conn=DBUtil.getConnection(); pstmt=conn.prepareStatement(sql); pstmt.setString(1, stu.getName()); pstmt.setInt(2, stu.getAge()); pstmt.setString(3, stu.getGender()); pstmt.setString(4, stu.getPhone()); pstmt.setString(5, stu.getEducation()); pstmt.setInt(6, stu.getId()); pstmt.executeUpdate(); System.out.println("修改學生成功!"); } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.closeAll(rs, pstmt, conn); } } @Override public List<Student> getAllStudents() { List<Student> students=new ArrayList<Student>(); String sql="select * from student"; try { conn=DBUtil.getConnection(); pstmt=conn.prepareStatement(sql); rs=pstmt.executeQuery(); while(rs.next()){ Student stu=new Student(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4),rs.getString(5),rs.getString(6)); students.add(stu); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.closeAll(rs, pstmt, conn); } return students; } @Override public Student getStudentById(int id) { Student stu=null; String sql="select * from student where id=?"; try { conn=DBUtil.getConnection(); pstmt=conn.prepareStatement(sql); pstmt.setInt(1, id); rs=pstmt.executeQuery(); if(rs.next()){ stu=new Student(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4),rs.getString(5),rs.getString(6)); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.closeAll(rs, pstmt, conn); } return stu; } @Override public List<Student> getStudentsByCondition(String name, String gender) { List<Student> students=new ArrayList<Student>(); String sql="select * from student where name like ? and gender=?"; try { conn=DBUtil.getConnection(); pstmt=conn.prepareStatement(sql); pstmt.setObject(1, "%"+name+"%");//注意此寫法 pstmt.setObject(2, gender); rs=pstmt.executeQuery(); while(rs.next()){ Student stu=new Student(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4),rs.getString(5),rs.getString(6)); students.add(stu); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.closeAll(rs, pstmt, conn); } return students; } }
  • 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
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 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
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127

實體類Student,POJO

/*
 * 實體類Student,POJO
 */
public class Student {
    private int id;
    private String name;
    private int age;
    private String gender;
    private String phone;
    private String education;

    public Student() {
        super();
    }

    public Student(int id, String name, int age, String gender, String phone,
            String education) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
        this.education = education;
    }

    public Student(String name, int age, String gender, String phone,
            String education) {
        super();
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
        this.education = education;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

}
  • 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
  • 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

測試類

import java.util.Iterator;
import java.util.List;
/*
 * 測試類
 */
public class Test {
    public static void main(String[] args) {
        //新增學生
        StudentDAO sd=new StudentDAOImpl();
        sd.addStudent(new Student("tom",20,"男","110","本科"));

        //刪除學生
        sd.deleteStudent(2);

        //修改學生
        sd.updateStudent(new Student(5, "湯姆", 25, "男", "112", "研究生"));

        //查詢所有學生
        List<Student> students=sd.getAllStudents();
        Iterator<Student> it=students.iterator();
        while(it.hasNext()){
            Student stu=it.next();
            System.out.println(stu.getId()+","+stu.getName()+","+stu.getEducation());
        }

        //根據編號查詢學生
        Student stu=sd.getStudentById(5);
        System.out.println(stu.getId()+","+stu.getName()+","+stu.getEducation());

        //模糊查詢
        List<Student> students=sd.getStudentsByCondition("姆", "男");
        Iterator<Student> it=students.iterator();
        while(it.hasNext()){
            Student stu=it.next();
            System.out.println(stu.getId()+","+stu.getName()+","+stu.getEducation());
        }
    }
}
  • 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
  • 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

資料庫工具類

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
 * 資料庫工具類
 */
public class DBUtil {
    //獲取資料庫連線
    public static Connection getConnection(){
        Connection conn=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    //關閉所有資源 
    public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
        try {
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(conn!=null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}