1. Viết phương thức truy vấn và thêm dữ liệu để tương tác với cơ sở dữ liệu MySQL
1.1 Phương thức truy vấn dữ liệu
public ArrayList<Student> IOPout() {
ArrayList<Student> danhSach = new ArrayList<>();
Connection ketNoi = null;
PreparedStatement cauLenh = null;
ResultSet ketQua = null;
try {
ketNoi = DBHelper.layKetNoi();
cauLenh = ketNoi.prepareStatement("select * from student");
ketQua = cauLenh.executeQuery();
while (ketQua.next()) {
Student sinhVien = new Student();
sinhVien.setMaSV(ketQua.getInt(1));
sinhVien.setTenSV(ketQua.getString(2));
sinhVien.setGioiTinh(ketQua.getString(3));
sinhVien.setTuoi(ketQua.getInt(4));
sinhVien.setDiaChi(ketQua.getString(5));
danhSach.add(sinhVien);
}
} catch (Exception e) {
e.printStackTrace();
}
return danhSach;
}
1.2 Phương thức thêm dữ liệu
public int them(Student s) {
int soDong = 0;
Connection ketNoi = null;
PreparedStatement cauLenh = null;
try {
ketNoi = DBHelper.layKetNoi();
cauLenh = ketNoi.prepareStatement("insert into student values (?,?,?,?,?)");
cauLenh.setDouble(1, s.getMaSV());
cauLenh.setString(2, s.getTenSV());
cauLenh.setString(3, s.getGioiTinh());
cauLenh.setDouble(4, s.getTuoi());
cauLenh.setString(5, s.getDiaChi());
soDong = cauLenh.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return soDong;
}
2. Data Export (Xuất Dữ Liệu)
2.1 Tạo phương thức main và实例 hóa lớp
CreExcl creExcl = new CreExcl();
// Thiết lập đường dẫn file Excel
File file = new File("D:/sinhVien.xlsx");
2.2 Gọi phương thức truy vấn và tạo file Excel
// Gọi phương thức truy vấn dữ liệu
ArrayList<Student> danhSach = creExcl.IOPout();
// Tạo workbook và sheet
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Thông Tin Sinh Viên");
2.3 Tạo hàng và thêm tiêu đề
// Tạo hàng đầu tiên (hàng tiêu đề)
XSSFRow hang = sheet.createRow(0);
hang.createCell(0).setCellValue("Mã Số");
hang.createCell(1).setCellValue("Tên");
hang.createCell(2).setCellValue("Giới Tính");
hang.createCell(3).setCellValue("Tuổi");
hang.createCell(4).setCellValue("Địa Chỉ");
2.4 Lặp qua dữ liệu và thêm vào sheet
// Lặp qua danh sách sinh viên và thêm vào sheet
for (int i = 0; i < danhSach.size(); i++) {
hang = sheet.createRow(i + 1);
hang.createCell(0).setCellValue(danhSach.get(i).getMaSV());
hang.createCell(1).setCellValue(danhSach.get(i).getTenSV());
hang.createCell(2).setCellValue(danhSach.get(i).getGioiTinh());
hang.createCell(3).setCellValue(danhSach.get(i).getTuoi());
hang.createCell(4).setCellValue(danhSach.get(i).getDiaChi());
}
2.5 Xuất dữ liệu ra file và đóng stream
try {
FileOutputStream stream = new FileOutputStream(file);
workbook.write(stream);
stream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
3. Data Import (Nhập Dữ Liệu)
3.1 Tạo input stream và workbook
// Tạo input stream
FileInputStream stream = new FileInputStream(file);
// Tạo workbook
Workbook workbook = new XSSFWorkbook(stream);
// Lấy sheet đầu tiên
Sheet sheet = workbook.getSheetAt(0);
3.2 Lặp qua các hàng và xử lý dữ liệu
// Lặp qua các hàng từ hàng 1 đến hàng cuối
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row hang = sheet.getRow(i);
if (hang != null) {
Student sinhVien = new Student();
// Xử lý và thêm dữ liệu
sinhVien.setMaSV((int) hang.getCell(0).getNumericCellValue());
sinhVien.setTenSV(hang.getCell(1).getStringCellValue());
sinhVien.setGioiTinh(hang.getCell(2).getStringCellValue());
sinhVien.setTuoi((int) hang.getCell(3).getNumericCellValue());
sinhVien.setDiaChi(hang.getCell(4).getStringCellValue());
// Gọi phương thức thêm sinh viên
creExcl.them(sinhVien);
}
}