-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentDaoImpl.java
133 lines (84 loc) · 3.17 KB
/
StudentDaoImpl.java
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
128
129
130
131
132
133
package com.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.bean.Student;
import com.config.DatabaseConnection;
import com.dao.IStudentDao;
public class StudentDaoImpl implements IStudentDao {
public List<Student> findAllStudent() throws Exception {
String sql = "select * from student";
Connection connection = DatabaseConnection.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet r=ps.executeQuery();
List<Student> students=new ArrayList<>();
while(r.next()) {
int no = r.getInt(2);
String name = r.getString(3);
int age = r.getInt(4);
String sex = r.getString(5);
String telephone = r.getString(6);
String major = r.getString(7);
Student student = new Student(no,name, age, sex, telephone,major);
students.add(student);
}
DatabaseConnection.releaseAll(connection,ps, r);
return students;
}
public Student findStudentByNo(Integer no) throws Exception {
String sql = "select * from student where Sno = ?";
Connection connection = DatabaseConnection.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,no);
ResultSet r=ps.executeQuery();
if(r.next()) {
int studentNo = r.getInt(2);
String name = r.getString(3);
int age = r.getInt(4);
String sex = r.getString(5);
String telephone = r.getString(6);
String major = r.getString(7);
Student student = new Student(studentNo,name, age, sex, telephone,major);
return student;
}
DatabaseConnection.releaseAll(connection,ps, r);
return null;
}
public void insertStudent(Student student) throws Exception {
String sql = "insert into student(Sno,Sname,Sage,Ssex,Stel,Smajor) values(?,?,?,?,?,?)";
Connection connection = DatabaseConnection.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,student.getNo());
ps.setString(2,student.getName());
ps.setInt(3,student.getAge());
ps.setString(4,student.getSex());
ps.setString(5,student.getTelephone());
ps.setString(6,student.getMajor());
ps.execute();
DatabaseConnection.release(connection,ps);
}
public void updateStudent(Integer no,Student student) throws Exception {
String sql = "update student set Sno = ?,Sname = ?,Sage = ?,Ssex = ?,Stel = ?,Smajor = ? where Sno = ?";
Connection connection = DatabaseConnection.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,student.getNo());
ps.setString(2,student.getName());
ps.setInt(3,student.getAge());
ps.setString(4,student.getSex());
ps.setString(5,student.getTelephone());
ps.setString(6,student.getMajor());
ps.setInt(7,no);
ps.execute();
DatabaseConnection.release(connection,ps);
}
public void deleteStudentByNo(Integer no) throws Exception {
String sql = "delete from student where Sno = ?";
Connection connection = DatabaseConnection.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,no);
ps.execute();
DatabaseConnection.release(connection,ps);
}
}