1.
Cấu hình SQL server:
2. Tạo Database bằng SQL Server
Tạo database PRJ_321 có table Emp_ID:
3. Tạo project, thêm thư viên sqljdbc42 vào Project
Chuột phải Libraries -> Chọn Add JAR/Folder…
Chỉ định thư mục chứa JDBC Driver và chọn tập tin [Link] -> chọn Open
(Nhiều trường hợp kết nối lỗi phải copy file sqljdbc_auth.dll bỏ vào thư mục
system32) mới chạy được. Lưu ý nếu dùng sqljdbc_8.4 thì copy file mssql-
jdbc_auth-[Link] trong thư mục auth vào Windows/System32 và nhớ ĐỔI
TÊN file thành sqljdbc_auth.dll.
4. Tạo phần model bằng JavaBean:
*Tạo class DatabaseInfo: chứa thông tin về Cơ sở dữ liệu
package model;
public interface DatabaseInfo {
String driverName = "[Link]";
String dbURL = "jdbc:sqlserver://localhost:1433;databaseName= PRJ321_ID;";
String dbUser = "sa";
String dbPass = "123456";
}
Hoặc Nếu SQL server sử dụng chứng thực theo windows
public static void KetnoiCSDL()
{
String dbURL = "jdbc:sqlserver://localhost\\SQLEXPRESS;
databaseName= QLSV;"
+"integratedSecurity=true; encrypt=true;"
+ "trustServerCertificate=true";
Connection conn = null;
try {
[Link]("[Link]");
conn = [Link](dbURL);//,
dbUser, dbPass);
[Link]("connect successfully!");
} catch (Exception ex) {
[Link]("connect failure!");
[Link]();
}
}
*Tạo class ConnectDB: kết nối đến Cơ sở dữ liệu (Việc kết nối có thể dùng
chung cho nhiều class nên thường tách thành file riêng):
package model;
import [Link];
import [Link];
public class ConnectDB implements DatabaseInfo {
public static Connection getConnection() {
Connection connection = null;
try {
//Tải driver
[Link](driverName);
//Kết nối
connection = [Link](dbURL, dbUser, dbPass);
[Link]("Connect database successful");
} catch (Exception e) {
[Link]("Failure");
[Link]();
return connection;
*Tạo class Employee liên kết đến table Emp_ID trong database:
package model;
public class Employee{
private String empID;
private String lastName;
private String firstName;
private String gender;
private String email;
public Employee() {
public Employee(String empID, String lastName, String firstName, String gender, String email) {
[Link] = empID;
[Link] = lastName;
[Link] = firstName;
[Link] = gender;
[Link] = email;
public String getEmpID() {
return empID;
public void setEmpID(String empID) {
[Link] = empID;
public String getLastName() {
return lastName;
public void setLastName(String lastName) {
[Link] = lastName;
public String getFirstName() {
return firstName;
public void setFirstName(String firstName) {
[Link] = firstName;
public String getGender() {
return gender;
public void setGender(String gender) {
[Link] = gender;
public String getEmail() {
return email;
public void setEmail(String email) {
[Link] = email;
*Tạo class EmployeeDB gồm các phương thức getList và newEmp:
package model;
import [Link].*;
import [Link];
import [Link];
public class EmployeeDB {
Connection connection=null;
public EmployeeDB() {
public Vector<Employee> getList() {
Vector employees = new Vector();
try {
String sql = "SELECT * FROM Emp_ID";
connection = [Link]();
Statement statement = [Link]();
ResultSet resultSet = [Link](sql);
while ([Link]()) {
Vector v=new Vector<>();
[Link]([Link](1));
[Link]([Link](2));
[Link]([Link](3));
[Link]([Link](4));
[Link]([Link](5));
[Link](v);
[Link]();
} catch (SQLException e) {
[Link]();
return employees;
//newEmp để thêm mới
public int newEmp(String empID, String lastName, String firstName, String gender, String email) {
int rc = 0;
try {
String sql = "INSERT INTO Emp_ID (empID, lastName, firstName, gender, email) VALUES (?, ?, ?, ?,
?)";
connection = [Link]();
PreparedStatement preparedStatement =
[Link](sql); [Link](1, empID);
[Link](2, lastName); [Link](3,
firstName); [Link](4, gender);
[Link](5, email); rc =
[Link]();
[Link]();
} catch (SQLException e)
{ [Link]();
return rc;
5.Xây dựng GUI để thêm và hiển thị Emp
Viết mã lệnh xử lý:
package view;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class EmpView extends [Link] {
EmployeeDB empDB = new EmployeeDB();
Vector<String> header = new Vector<>();
Vector data = new Vector();
public EmpView() {
initComponents();
//Thêm header
[Link]("Employee ID");
[Link]("Last Name");
[Link]("First Name");
[Link]("Gender");
[Link]("Email");
[Link](true);
//Thêm Emp
private void btnNewActionPerformed([Link] evt)
{ //Lấy dữ liệu nhập vào
String empID = [Link]();
String lastName = [Link]();
String firstName = [Link]();
String email = [Link]();
String gender = [Link]() ? "M" : "F";
//Kiểm tra ID hợp lệ
if () [Link](null, "Employee ID: DE + 6
digits.");
//Kiểm tra email hợp lệ
else if ({1,2}$"))
[Link](null, "Email: characters + @ + characters + .com");
else
Employee employee = [Link](empID);//Kiểm tra trùng khóa
if (employee == null) {
int n = [Link](empID, lastName, firstName, gender,
email); if (n == 1) {
//Hiển thị lại dữ liệu mới nếu thêm thành công
btnViewActionPerformed(null);
} else //Không thêm được
[Link](this, "Add employee failure!");
} else //Trùng khóa
[Link](this, "Duplicate key!");
//Lấy tất cả dữ liệu hiển thị ra table
private void btnViewActionPerformed([Link] evt) {
//Lấy dữ liệu Emp
data = [Link]();
//Hiển thị ra table
DefaultTableModel tblModel;
tblModel = (DefaultTableModel)
[Link](); [Link](data,
header);
//Thoát
private void btnExitActionPerformed([Link] evt) {
[Link](0);