Java
Java
StudentDAO;
import GUI.ViewStudentList;
import Model.Student;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
package Model;
@Override
public String toString() {
return "Model.SinhVien{" +
"mssv='" + mssv + '\'' +
", ten='" + ten + '\'' +
", lop='" + lop + '\'' +
'}';
}
public String getMssv() {
return mssv;
}
package JDBCHelper;
import javax.swing.*;
import java.sql.*;
static {
try {
instance = DatabaseConnection.getConnection();
} catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(
null,
"Không thể kết nối đến cơ sở dữ liệu. Hãy chạy lại ứng dụng!",
"Lỗi",
JOptionPane.ERROR_MESSAGE
);
e.printStackTrace();
System.exit(1);
}
}
private DatabaseConnection() {
}
package GUI;
import Model.Student;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.util.List;
@Override
public void removeUpdate(DocumentEvent e) {
var text = textFieldStudent.getText().strip();
if (text.length() != 0) {
rowSorter.setRowFilter(RowFilter.regexFilter(text, 1));
} else {
rowSorter.setRowFilter(null);
}
}
@Override
public void changedUpdate(DocumentEvent e) {
}
});
}
package DAO;
import JDBCHelper.DatabaseConnection;
import Model.Student;
import java.sql.SQLException;
try {
e.printStackTrace();
return conn;
try {
pstm = conn.prepareStatement(query);
pstm.setString(1, "ngoc");
ResultSet rs = pstm.executeQuery();
int id = rs.getInt("id");
System.out.println(id + " - " + username + " - " + password + " - " + email);
conn.close();
} catch (SQLException e) {
e.printStackTrace();
try {
pstm = conn.prepareStatement(query);
pstm.setString(1, "hung");
pstm.setString(2, "123456789");
pstm.setString(3, "hung@gmail.com");
//Khi thực hiện các lệnh insert/update/delete sử dụng executeUpdate(), nó sẽ trả về số hàng
bị tác động
if(row != 0){
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
Lớp này để lưu thông tin cho mỗi sinh viên.
package vn.viettuts;
import java.io.Serializable;
/**
* Student class
*
* @author viettuts.vn
*/
public class Student implements Serializable {
private int id;
private String name;
private byte age;
private String address;
/* điểm trung bình của sinh viên */
private float gpa;
public Student() {
}
public Student(int id, String name, byte age,
String address, float gpa) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.gpa = gpa;
}
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 byte getAge() {
return age;
}
public void setAge(byte age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public float getGpa() {
return gpa;
}
public void setGpa(float gpa) {
this.gpa = gpa;
}
}
package vn.viettuts;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* StudentDao class
*
* @author viettuts.vn
*/
public class StudentDao {
private static final String STUDENT_FILE_NAME = "student.txt";
/**
* save list student to file
*
* @param studentList: list student to save
*/
public void write(List<Student> studentList) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(new File(STUDENT_FILE_NAME));
oos = new ObjectOutputStream(fos);
oos.writeObject(studentList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeStream(fos);
closeStream(oos);
}
}
/**
* read list student from file
*
* @return list student
*/
public List<Student> read() {
List<Student> studentList = new ArrayList<>();
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(new File(STUDENT_FILE_NAME));
ois = new ObjectInputStream(fis);
studentList = (List<Student>) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
closeStream(fis);
closeStream(ois);
}
return studentList;
}
/**
* close input stream
*
* @param is: input stream
*/
private void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* close output stream
*
* @param os: output stream
*/
private void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// sắp xếp sinh viên tăng dần theo điểm trung bình
package vn.viettuts;
import java.util.Comparator;
/**
* SortStudentByGPA class
*
* @author viettuts.vn
*/
public class SortStudentByGPA implements Comparator<Student> {
@Override
public int compare(Student student1, Student student2) {
if (student1.getGpa() > student2.getGpa()) {
return 1;
}
return -1;
}
}
package vn.viettuts;
import java.util.Comparator;
/**
* SortStudentByName class
*
* @author viettuts.vn
*/
public class SortStudentByName implements Comparator<Student> {
@Override
public int compare(Student student1, Student student2) {
return student1.getName().compareTo(student2.getName());
}
}
/////////////
Tao lop main
package vn.viettuts;
import java.util.Scanner;
/**
* Main class
*
* @author viettuts.vn
*/
public class Main {
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String choose = null;
boolean exit = false;
StudentManager studentManager = new StudentManager();
int studentId;
// show menu
showMenu();
while (true) {
choose = scanner.nextLine();
switch (choose) {
case "1":
studentManager.add();
break;
case "2":
studentId = studentManager.inputId();
studentManager.edit(studentId);
break;
case "3":
studentId = studentManager.inputId();
studentManager.delete(studentId);
break;
case "4":
studentManager.sortStudentByGPA();
break;
case "5":
studentManager.sortStudentByName();
break;
case "6":
studentManager.show();
break;
case "0":
System.out.println("exited!");
exit = true;
break;
default:
System.out.println("invalid! please choose action in below menu:");
break;
}
if (exit) {
break;
}
// show menu
showMenu();
}
}
/**
* create menu
*/
public static void showMenu() {
System.out.println("-----------menu------------");
System.out.println("1. Add student.");
System.out.println("2. Edit student by id.");
System.out.println("3. Delete student by id.");
System.out.println("4. Sort student by gpa.");
System.out.println("5. Sort student by name.");
System.out.println("6. Show student.");
System.out.println("0. exit.");
System.out.println("---------------------------");
System.out.print("Please choose: ");
}
}
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import java.sql.*;
System.out.println(conn.getClientInfo());
// addddddđ
try {
PreparedStatement pstm1 = conn.prepareStatement("INSERT INTO
IDSinhVien(Ten,Diem) VALUES(?,?)");
pstm1.setString(1,"Loi1");
pstm1.setInt(2,8);
pstm1.executeUpdate();
}catch (SQLException e1){
e1.printStackTrace();
}
try {
try {
PreparedStatement pstm = conn.prepareStatement("SELECT * From IDSinhVien");
ResultSet rs = pstm.executeQuery();
while (rs.next()){
System.out.println("id :"+rs.getInt("ID"));
System.out.println("Ten :"+rs.getString("Ten"));
System.out.println("Diem :"+ rs.getInt("Diem"));
}
} catch (SQLException e2) {
e2.printStackTrace();
}
} catch (SQLServerException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}