Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2b6e3db

Browse files
committed
Add Student management UI and controller classes for handling student data
1 parent 623e059 commit 2b6e3db

File tree

7 files changed

+454
-9
lines changed

7 files changed

+454
-9
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package controller;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
import java.util.Vector;
6+
7+
import models.Student;
8+
import models.StudentManager;
9+
import views.StudentUI;
10+
11+
public class StudentController {
12+
private StudentManager studentManager; // models
13+
private StudentUI studentUI; // views
14+
15+
public StudentController() {
16+
studentManager = new StudentManager();
17+
studentUI = new StudentUI();
18+
19+
loadDada();
20+
solveActionListener();
21+
22+
}
23+
24+
public void loadDada() {
25+
Vector vData = studentManager.convertToVector();
26+
studentUI.setDataModelTable(vData);
27+
}
28+
29+
public void solveActionListener() {
30+
studentUI.getAddButton().addActionListener(new ActionListener() {
31+
public void actionPerformed(ActionEvent e) {
32+
// Lấy thông tin từ giao diện
33+
String masv = studentUI.getMaSvTextField().getText();
34+
String name = studentUI.getNameTextField().getText();
35+
int age = (int) studentUI.getTuoiSpinner().getValue();
36+
String gender = "Nam";
37+
if (studentUI.getNuRadio().isSelected())
38+
gender = "Nữ";
39+
// Tạo 1 đối tượng
40+
Student student = new Student(age, masv, name, age, gender);
41+
// Thêm đối tượng vào database
42+
studentManager.create(student);
43+
// Load lại thông tin
44+
loadDada();
45+
46+
}
47+
});
48+
49+
}
50+
}

‎Khoá học Java Swing và JDBC T11/05_java_jdbc/TestJDBC/src/models/ConnectSQL.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ private ConnectSQL() {
1818
System.out.println("Đã kết nối tới SQL");
1919
} catch (Exception e) {
2020
// TODO: handle exception
21-
System.out.println(e.getMessage());
21+
// System.out.println(e.getMessage());
22+
e.printStackTrace();
2223
}
2324
}
2425

‎Khoá học Java Swing và JDBC T11/05_java_jdbc/TestJDBC/src/models/Student.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package models;
22

3+
import java.util.Scanner;
4+
import java.util.Vector;
5+
36
public class Student {
47
private int id;
58
public String masv;
@@ -28,6 +31,30 @@ public Student(int id, String masv, String name, int age, String gender) {
2831
public String toString() {
2932
return "Student [id=" + id + ", masv=" + masv + ", name=" + name + ", age=" + age + ", gender=" + gender + "]";
3033
}
34+
35+
public void input() {
36+
Scanner sc = new Scanner(System.in);
37+
System.out.println("Nhập Mã SV: ");
38+
this.masv = sc.nextLine();
39+
System.out.println("Nhập Tên: ");
40+
this.name = sc.nextLine();
41+
System.out.println("Nhập Age: ");
42+
this.age = sc.nextInt();
43+
sc.nextLine();
44+
System.out.println("Nhập Gender: ");
45+
this.gender = sc.nextLine();
46+
47+
}
48+
49+
public Vector convertVector() {
50+
Vector v = new Vector<>();
51+
v.add(this.id);
52+
v.add(this.masv);
53+
v.add(this.name);
54+
v.add(this.getAge());
55+
v.add(this.gender);
56+
return v;
57+
}
3158

3259
public String getMasv() {
3360
return masv;

‎Khoá học Java Swing và JDBC T11/05_java_jdbc/TestJDBC/src/models/StudentManager.java‎

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
55
import java.sql.ResultSet;
66
import java.sql.SQLException;
77
import java.util.ArrayList;
8+
import java.util.Vector;
89

910
public class StudentManager {
10-
public static ArrayList<Student> getAll() {
11+
12+
private ArrayList<Student> students;
13+
14+
public StudentManager() {
15+
this.students = this.getAll();
16+
}
17+
18+
// Lấy dữ liệu từ bảng sinh viên
19+
public ArrayList<Student> getAll() {
20+
1121
ArrayList<Student> students = new ArrayList<>();
1222
try {
1323
Connection conn = ConnectSQL.getConnected();
@@ -33,7 +43,8 @@ public static ArrayList<Student> getAll() {
3343
return students;
3444
}
3545

36-
public static void create(Student s) {
46+
// Thêm dữ liệu vào từ bảng sinh viên
47+
public void create(Student s) {
3748

3849
try {
3950
Connection conn = ConnectSQL.getConnected();
@@ -47,6 +58,7 @@ public static void create(Student s) {
4758
int row = ptsm.executeUpdate();
4859
if (row > 0) {
4960
System.out.println("Thêm thành công " + s);
61+
students.add(s);
5062
} else {
5163
System.out.println("Thêm lỗi");
5264
}
@@ -56,4 +68,79 @@ public static void create(Student s) {
5668
System.out.println(e.getMessage());
5769
}
5870
}
71+
72+
// Sửa dữ liệu
73+
public void update(int id, Student s) {
74+
try {
75+
Connection conn = ConnectSQL.getConnected();
76+
String query = "UPDATE students SET masv=?, name=?, age=?, gender=? WHERE id=?";
77+
PreparedStatement ptsm = conn.prepareStatement(query);
78+
ptsm.setString(1, s.getMasv());
79+
ptsm.setString(2, s.getName());
80+
ptsm.setInt(3, s.getAge());
81+
ptsm.setString(4, s.getGender());
82+
ptsm.setInt(5, id);
83+
84+
int row = ptsm.executeUpdate();
85+
if (row > 0) {
86+
System.out.println("Sửa thành công " + s);
87+
} else {
88+
System.out.println("Sửa bị lỗi");
89+
}
90+
91+
} catch (SQLException e) {
92+
// TODO Auto-generated catch block
93+
System.out.println(e.getMessage());
94+
}
95+
96+
}
97+
98+
// Xoá sinh viên
99+
public void delete(int id) {
100+
try {
101+
Connection conn = ConnectSQL.getConnected();
102+
String query = "DELETE FROM students WHERE id=?";
103+
PreparedStatement ptsm = conn.prepareStatement(query);
104+
ptsm.setInt(1, id);
105+
106+
int row = ptsm.executeUpdate();
107+
if (row > 0) {
108+
System.out.println("Xoá thành công ");
109+
} else {
110+
System.out.println("Xoá bị lỗi");
111+
}
112+
113+
} catch (SQLException e) {
114+
// TODO Auto-generated catch block
115+
System.out.println(e.getMessage());
116+
// e.printStackTrace();
117+
}
118+
}
119+
120+
// Tìm sinh viên dựa vào id
121+
public Student find(int id) {
122+
this.students = getAll();
123+
for (Student s: students)
124+
if (s.getId() == id) {
125+
return s;
126+
}
127+
return null;
128+
}
129+
130+
131+
// Hiển thị danh sách sinh viên
132+
public void display() {
133+
this.students = getAll();
134+
for (Student s: students) {
135+
System.out.println(s);
136+
}
137+
}
138+
139+
public Vector convertToVector() {
140+
this.students = getAll();
141+
Vector vData = new Vector<>();
142+
for (Student s: students)
143+
vData.add(s.convertVector());
144+
return vData;
145+
}
59146
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package test;
2+
3+
import java.util.Scanner;
4+
5+
import models.Student;
6+
import models.StudentManager;
7+
8+
public class Menu {
9+
10+
public static int chooseMenu() {
11+
Scanner sc = new Scanner(System.in);
12+
System.out.println("+------- Menu ---------+");
13+
System.out.println("| 1. Thêm sinh viên ");
14+
System.out.println("| 2. In ra danh sách sinh viên");
15+
System.out.print("Nhập lựa chọn: ");
16+
return sc.nextInt();
17+
}
18+
19+
public static void main(String[] args) {
20+
StudentManager studentManager = new StudentManager();
21+
int choose;
22+
do {
23+
choose = chooseMenu();
24+
switch (choose) {
25+
case 1: {
26+
Student s = new Student();
27+
s.input();
28+
studentManager.create(s);
29+
break;
30+
}
31+
case 2: {
32+
studentManager.display();
33+
break;
34+
}
35+
}
36+
37+
} while (choose != 0);
38+
System.out.println("Đã thoát");
39+
40+
}
41+
42+
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package test;
22

33
import java.util.ArrayList;
4+
5+
import controller.StudentController;
46
import models.Student;
57
import models.StudentManager;
68

79
public class Test {
810

911
public static void main(String[] args) {
1012
// TODO Auto-generated method stub
11-
12-
Student s = new Student(0, "24IT395", "Nguyễn Quang Vũ", 38, "Nam");
13-
StudentManager.create(s);
1413

15-
ArrayList<Student> students = StudentManager.getAll();
16-
for (int i = 0; i < students.size(); i++)
17-
System.out.println(students.get(i));
14+
StudentController studentController = new StudentController();
1815
}
1916

2017
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /