-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentDAO.java
More file actions
102 lines (79 loc) · 2.83 KB
/
Copy pathStudentDAO.java
File metadata and controls
102 lines (79 loc) · 2.83 KB
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
package connnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class StudentDAO {
private static StudentDAO instace = new StudentDAO();
public static StudentDAO getInstance() {
return instace;
}
public boolean insert(Student t) {
try {
Connection connection = JDBCConnection.getJDBCConnection();
String sql = "INSERT INTO STUDENT(NAME, GENDER, COUNTRY, AGE)"
+ " VALUES(?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, t.getName());
statement.setString(2, t.getGender());
statement.setString(3, t.getCountry());
statement.setInt(4, t.getAge());
int check = statement.executeUpdate();
if (check > 0) {
System.out.println("them du lieu thanh cong");
} else {
System.out.println("Them du lieu that bai");
}
JDBCConnection.closeConnection(connection);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("Nhập sai định dạng");
return false;
}
return true;
}
public ArrayList<Student> selectAll() {
ArrayList<Student> students = new ArrayList<>();
try {
Connection connection = JDBCConnection.getJDBCConnection();
String sql = "SELECT * FROM STUDENT";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("ID");
String name = resultSet.getString("NAME");
String gender = resultSet.getString("GENDER");
String country = resultSet.getString("COUNTRY");
int age = resultSet.getInt("AGE");
students.add(new Student(id, name, gender, country, age));
}
JDBCConnection.closeConnection(connection);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return students;
}
public int getSequence() {
int sequence = 0;
try {
Connection connection = JDBCConnection.getJDBCConnection();
String sql = "SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME = 'student'";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
sequence = resultSet.getInt("AUTO_INCREMENT");
}
JDBCConnection.closeConnection(connection);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sequence;
}
public static void main(String[] args) {
// System.out.println(StudentDAO.getInstance().getSequence());
// StudentDAO.getInstance().insert(new Student("Student", "male", "Thanh Hoa", 23));
}
}