Full khóa học lập trình cơ sở dữ liệu với Java

Để học được khóa Lập trình Cơ sở dữ liệu với Java này bắt buộc các bạn phải có kiến thức về lập trình hướng đối tượng với Java trước. Trong chương trình tặng 14 khóa học khi cung cấp minh chứng ủng hộ quỹ vaccine của Chính Phủ Tui đã tặng các bạn rồi, bây giờ Tui tặng thêm khóa Lập trình cơ sở dữ liệu với Java, Khóa này Tui publish trên youtube các bạn tham khảo.

Muốn học khóa lập trình hướng đối tượng với thì đăng ký học Java OOP tại đây.

hoặc muốn học các khóa khác thì vào https://unica.vn/?aff=11929 tìm Trần Duy Thanh, rồi chọn Khóa học phù hợp

Ngoài ra Tui có viết các bài Blog khá chi tiết về Java Backend, các bạn có thể tham khảo tại đây:

https://duythanhcse.wordpress.com/java/java-backend/

Các câu hỏi về lập trình vui lòng vào public group:
https://www.facebook.com/groups/communityuni/

Chương trình học cụ thể của Khóa này như sau:

Khóa học cung cấp kiến thức nâng cao của ngôn ngữ lập trình java .
-Xử lý giao diện
-Tiếp cận được 4 loại hệ cơ sở dữ liệu phổ biến: Microsoft Access, Microsoft SQL Server, MySQL và Oracle.
-Nắm chắc các kiến thức về JDBC như truy vấn, cập nhật, xóa dữ liệu…
-Tiếp cận được JPA để nâng cao hiệu suất lập trình cơ sở dữ liệu với Java
-Sau khi hoàn thành khóa học, có thể tự viết được dự án liên quan tới quản trị cơ sở dữ liệu.

Học liệu: Bao gồm Slide từng bài học và source code của từng bài học, tải ở đây.

Full link khóa học Lập trình cơ sở dữ liệu với Java trên Youtube:

Số lượng Video: 63

Thời lượng: 24 giờ 33 phút

Dung lượng: 55.9 GB Full HD

Video theo từng bài học:

STTBài họcLink học
Chương ILập trình Java OOP (đã tặng) 
  https://unica.vn/lap-trinh-java-trong-4-tuan?aff=11929  (đã tặng)
Chương 2Lập trình xử lý giao diện cơ bản trong java 
Bài 1Giới thiệu gói Swing, lớp JFrame,Jpanel Link học
Bài 2Các loại Layout Manager quan trọng thường dùng  Link học
Bài 3o        JLabel
o        JTextField
o        JButton
– Các kỹ thuật gán sự kiện
  Link học
Bài 4o        Cách tạo Border, Border Title
o        JTextArea, JScrollPane
o        JCheckBox, JradioButton, ButtonGroup
  Link học
Bài 5o        JCombobox, JList (demo trên dữ liệu cơ sở và đối tượng)  Link học
Bài 6Project thực tế  Link học
Chương 3Lập trình xử lý giao diện nâng cao trong java 
Bài 1o        Split Panes, Jtable  Link học
Bài 2JTree  Link học
Bài 3o        JMenuBar
o        ContextMenu
o        JToolbar
  Link học
Bài 4o JTabbedPane  Link học
Bài 5Các loại Dialog thường dùng  JoptionPane, JFileChooser…  Link học
Bài 6Project thực tế  Link học
Chương 4Lập trình Cơ sở dữ liệu với Microsoft Access 
Bài 1Giới thiệu về Microsoft Access  Link học
Bài 2Cách kết nối Microsoft Access bằng ucanaccess  Link học
Bài 3Sử dụng Statement để truy vấn dữ liệu trong Microsoft Access  Link học
Bài 4Sử dụng Statement để thêm mới dữ liệu trong Microsoft Access  Link học
Bài 5Sử dụng Statement để thay đổi dữ liệu trong Microsoft Access  Link học
Bài 6Sử dụng Statement để xóa bỏ dữ liệu trong Microsoft Access  Link học
Bài 7Sử dụng PreparedStatement để truy vấn dữ liệu trong Microsoft Access  Link học
Bài 8Sử dụng PreparedStatement  để thêm mới dữ liệu trong Microsoft Access  Link học
Bài 9Sử dụng PreparedStatement  để thay đổi dữ liệu trong Microsoft Access  Link học
Bài 10Sử dụng PreparedStatement  để xóa bỏ dữ liệu trong Microsoft Access  Link học
Bài 11Project thực tế  Link học
Chương 5Lập trình Cơ sở dữ liệu với Microsoft SQL Server 
Bài 1Giới thiệu về hệ quản trị cơ sở dữ liệu Microsoft SQL Server  Link học
Bài 2Cách kết nối Microsoft SQL Server bằng SqlJDBC  Link học
Bài 3Sử dụng Statement để truy vấn dữ liệu trong Microsoft SQL Server  Link học
Bài 4Sử dụng Statement để thêm mới dữ liệu trong Microsoft SQL Server  Link học
Bài 5Sử dụng Statement để thay đổi dữ liệu trong Microsoft SQL Server  Link học
Bài 6Sử dụng Statement để xóa bỏ dữ liệu trong Microsoft SQL Server  Link học
Bài 7Sử dụng PreparedStatement để truy vấn dữ liệu trong Microsoft SQL Server  Link học
Bài 8Sử dụng PreparedStatement  để thêm mới dữ liệu trong Microsoft SQL Server  Link học
Bài 9Sử dụng PreparedStatement  để thay đổi dữ liệu trong Microsoft SQL Server  Link học
Bài 10Sử dụng PreparedStatement  để xóa bỏ dữ liệu trong Microsoft SQL Server  Link học
Bài 11Sử dụng CallableStatement để truy suất Store Procedure trong Microsoft SQL Server  Link học
Bài 12Project thực tế  Link học
Chương 6Lập trình Cơ sở dữ liệu với MySQL 
Bài 1Giới thiệu về hệ quản trị cơ sở dữ liệu MysQL  Link học
Bài 2Cách kết nối MySQL bằng MySql JDBC  Link học
Bài 3Sử dụng Statement để truy vấn dữ liệu trong MySql  Link học
Bài 4Sử dụng Statement để thêm mới dữ liệu trong MySql  Link học
Bài 5Sử dụng Statement để thay đổi dữ liệu trong MySql  Link học
Bài 6Sử dụng Statement để xóa bỏ dữ liệu trong MySql  Link học
Bài 7Sử dụng PreparedStatement để truy vấn dữ liệu trong MySql  Link học
Bài 8Sử dụng PreparedStatement  để thêm mới dữ liệu trong MySql  Link học
Bài 9Sử dụng PreparedStatement  để thay đổi dữ liệu trong MySql  Link học
Bài 10Sử dụng PreparedStatement  để xóa bỏ dữ liệu trong MySql  Link học
Bài 11Project thực tế  Link học
Chương 7Lập trình Cơ sở dữ liệu với Oracle 
Bài 1Giới thiệu về hệ quản trị cơ sở dữ liệu Oracle  Link học
Bài 2Cách kết nối Oracle bằng oJDBC7  Link học
Bài 3Sử dụng Statement để truy vấn dữ liệu trong Oracle  Link học
Bài 4Sử dụng Statement để thêm mới dữ liệu trong Oracle  Link học
Bài 5Sử dụng Statement để thay đổi dữ liệu trong Oracle  Link học
Bài 6Sử dụng Statement để xóa bỏ dữ liệu trong Oracle  Link học
Bài 7Sử dụng PreparedStatement để truy vấn dữ liệu trong Oracle  Link học
Bài 8Sử dụng PreparedStatement  để thêm mới dữ liệu trong Oracle  Link học
Bài 9Sử dụng PreparedStatement  để thay đổi dữ liệu trong Oracle  Link học
Bài 10Sử dụng PreparedStatement  để xóa bỏ dữ liệu trong Oracle  Link học
Bài 11Project thực tế  Link học
Chương 8Lập trình cơ sở dữ liệu với JPA 
Bài 1Giới thiệu JPA(Java Persistence API)  Link học
Bài 2Triển khai dự án quản lý Khách hàng bằng Hibernate-JPA (trên hệ cơ sở dữ liệu SQL Server)  Link học
Chương 9Giới thiệu về Windowbuilder Pro Eclipse 
Bài 1Cách cài đặt plug in Windowbuilder Pro cho Eclipse  Link học
Bài 2Cách sử dụng gói Windowbuilder Pro Eclipse  Link học
Chương 10Tổng kết khóa học 
Bài 1Kết xuất dự án ra file jar  Link học

Ngoài ra Tui có viết các bài Blog khá chi tiết về Java Backend, các bạn có thể tham khảo tại đây: https://duythanhcse.wordpress.com/java/java-backend/

Ví dụ đa tiến trình trong Java – thuật toán sắp xếp

Trong ví dụ này Tôi muốn demo cho các bạn các thuật toán sắp xếp bằng ngôn ngữ Java

Nếu như không dùng đa tiến trình thì giao diện của bạn sẽ bị “Đơ”

Source code: http://www.mediafire.com/?oee8cznyolwng5g

File Jar để chạy: http://www.mediafire.com/?03sxkpwa40icrp3

Ở đây Tôi đã làm xong thuật toán Bubble Sort, các thuật toán khác Tôi viết sẵn hàm, các bạn bắt chước Tôi làm để sửa lại cho đúng thuật toán sắp xếp khác.

Hình minh hoạ thuật toán đang tiến hành :

Hình minh họa đã hoàn thành thuật toán:

Các thuật toán Tôi đang để trống là:

public synchronized void doSelectionSort()
{

}
public synchronized void doInsertionSort()
{

}
public synchronized void doQuickSort()
{

}

Các bạn viết code trong này, việc xử lý khi người sử dụng chọn kiểu sắp xếp Tôi đã làm trong MyNumberFrame

Good luck!

Phần 1 – Kết nối các hệ cơ sở dữ liệu bằng Java

Yêu cầu: Các bạn phải rành về lập trình hướng đối tượng, tính kế thừa, tính đa hình thì mới hiểu được các phần hướng dẫn bên dưới. Tức là bạn phải khá về lập trình Java 1, nếu đọc mà vẫn không hiểu thì có thể email tới tranduythanh@hui.edu.vn Tôi sẽ giải thích những phần các bạn chưa hiểu.

———————————————————————————————————————

Phần 1: Kết nối Microsoft Access bằng Java

Phần 2: Kết nối Microsoft SQL Server 2008 bằng Java

Phần 3: Kết nối MySql bằng Java

———————————————————————————————————————

Topic này Tôi muốn hướng dẫn các bạn cách kết nối tới nhiều cơ sở dữ liệu khác nhau bằng ngôn ngữ lập trình Java

Các bạn sẽ được học đầy đủ trong môn Lập Trình Java 2.

Trong Topic này Tôi đặt ra một trường hợp là: Chúng ta có nhiều hệ cơ sở dữ liệu khác nhau ví dụ như Ms Access, SqlServer, MySQL… làm cách nào để viết các class Java thành các thư viện để kết nối tới các hệ cơ sở dữ liệu này?

Có rất nhiều mô hình, ở đây Tôi chỉ nhấn mạnh vào cách viết class để kết nối tới các CSDL mà thôi (không đề cập mô hình nào cả). Khi bạn rành rồi thì có thể sử dụng ORM hibernate để coding hoặc bất cứ loại nào. Nhưng trước tiên các bạn phải biết những kỹ thuật căn bản trước đã.

——————————————————————————————————————————————

Phần 1: Kết nối Microsoft Access bằng Java

Nếu bạn sử dụng MS Access 2003(đuôi là .mdb) thì không phải cài đặt thêm Driver

Còn nếu như bạn sử dụng MS Access 2007, 20120 (đuôi là .accdb)  thì các bạn cài đặt thêm:

2007 Office System Driver: Data Connectivity Components :

http://www.microsoft.com/download/en/details.aspx?id=23734

Những class nào bạn cần quan tâm trong phần 1 này:

Trong Connector bạn quan tâm tới classs CMsAccessConnector

Trong UI bạn quan tâm tới ConnectMsAccessUI

Tôi có chụp cấu trúc thư mục Project này như sau:

Mô hình class: Ở đây tôi tách làm 3 package để cho dễ quản lý.

– package Connector bao gồm các class kết nối và tương tác với các hệ cơ sở dữ liệu

– package Model dùng để mô hình hóa bảng dữ liệu thành các đối tượng.

– package UI cung cấp giao diện cho người sử dụng để tương tác với dữ liệu.

Mô hình của package Connector:

Mô hình của package Model:

Mô hình của package UI:

Cơ sở dữ liệu mẫu, ở đây Tôi làm 2 bảng lophoc và sinhvien ( các bạn chú ý là kiểu dữ liệu và kích thước là tương ứng cho cả 3 hệ cơ sở dữ liệu: Ms access, MySql và SqlServer…). Tôi muốn Demo sơ sơ chương trình quản lý sinh viên – lớp học.

Các bạn xem mô tả:

Trên đây là Tôi dùng Ms Access. Tương tự bạn làm cho Mysql, SqlServer. Có thể các bạn chưa thao tác với MySql bao giờ, Tôi sẽ hướng dẫn cách bạn download, cài đặt, cấu hình, sử dụng MySql Workbrench ở phần kế tiếp. Hiện tại bây giờ các bạn theo dõi Tôi hướng dẫn MS Access

Giao diện chính của chương trình như sau:

Tương ứng với mỗi nút lệnh “MS Access”, “SQL Server” , “My Sql” thì chương trình của chúng ta sẽ kết nối tới các hệ cơ sở dữ liệu đó.

Tôi sẽ giải thích chi tiết cách kết nối, cách tương tác với dữ liệu: Lấy dữ liệu, thêm, sửa, xóa, xử lý thống kê (in ấn dữ liệu)

Topic tương đối dài nên Tôi sẽ chia ra từng loại hệ cơ sở dữ liệu khác nhau. Chỉ cần các bạn cố gắng hiểu được cách tương tác tới 1 hệ cơ sở dữ liệu nào đó thì các hệ khác cũng tương tự, vấn đề chỉ nằm ở Logic lập trình của các bạn.

– Trong trường hợp kết nối tới MS Access, khi các bạn click chuột vào “Ms Access” thì sẽ có giao diện bên dưới:

Các bạn chú ý rằng. Giao diện trên được làm hết trong class AbstractConnectUI, Kể cả việc hiển thị dữ liệu.

Các bạn nhớ rằng. Vì chúng ta đang cố gắng tương tác với các hệ cơ sở dữ liệu khác nhau, tức là chỉ khác nhau ở nơi lưu trữ dữ liệu, còn giao diện và các nghiệp vụ thì hoàn toàn giống nhau.

Như vậy ứng với Ms Access thì trong ConnectMsAccessUI, bạn sẽ gọi các class Connector liên quan tới Ms Accesss.

hoặc là ứng với MySQL thì trong ConnectMySqlUI, bạn sẽ gọi các clsss Connector liên quan tới MySQL

Tức là chúng ta chỉ làm GIAO DIỆN 1 lần mà thôi, tùy vào việc sử dụng hệ cơ sở dữ liệu nào mà ta thay đổi hành vi khác nhau.

Các bạn phải rành về lập trình hướng đối tượng, tính kế thừa, tính đa hình thì việc tiếp thu sẽ nhanh chóng hơn.

Tôi đính kèm coding dưới này để các bạn tham khảo, Nếu như các bạn hiểu cách lấy dữ liệu từ Access rồi thì MySQl, SQL Server cũng sẽ tương tự. Topic này chỉ dừng lại ở việc truy suất dữ liệu. Trong ConnectMySqlUI, ConnectSqlServerUI Tôi cũng đã viết sẵn Code để truy vấn tới dữ liệu. Nếu bạn nào khá về Logic thì cố gắng đưa lên giao diện như trong Access, vài ngày nữa Tôi sẽ làm tiếp phần: Thêm, Sửa, Xóa, In ấn.

=================================================================

Package: tranduythanh.com.connector

=================================================================

class CConnector

package tranduythanh.com.connector;import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public abstract class CConnector

{

protected Connection m_Connection=null;

/**Hàm này dùng để kết nối tới các hệ sở dữ liệu

* Tùy vào từng Hệ CSDL chúng ta coding khác nhaucác lớp kế thừa từ

*/

public abstract Connection getConnect(String strServer,String strDatabase,String strUser,String strPwd);

/** Hàm này dùng để truy vấn dữ liệu,

* Connection con: đối tượng connnection

* String strSql: câu truy vấn

* Trả về ResultSet

*/

public ResultSet execQuery(Connection con,String strSql)

{

ResultSet rs=null;

try

{

Statement st=con.createStatement();

rs =st.executeQuery(strSql);

}

catch(SQLException ex)

{

ex.printStackTrace();

}

return rs;

}

/** Hàm này dùng để truy vấn dữ liệu,

* String strSql: câu truy vấn

* Trả về ResultSet

*/

public ResultSet execQuery(String strSql)

{

ResultSet rs=null;

try

{

Statement st=m_Connection.createStatement();

rs =st.executeQuery(strSql);

}

catch(SQLException ex)

{

ex.printStackTrace();

}

return rs;

}

/** Hàm này dùng cập nhật dữ liệu,

* Connection con: đối tượng connnection

* String strSql: câu truy vấn

* Nếu cập nhật thành công thì kết quả >0

*/

public int execNoneQuery(Connection con,String strSql)

{

int rec=0;

try

{

Statement st=con.createStatement();

rec=st.executeUpdate(strSql);

}

catch(SQLException ex)

{

ex.printStackTrace();

}

return rec;

}

/** Hàm này dùng cập nhật dữ liệu,

* String strSql: câu truy vấn

* Nếu cập nhật thành công thì kết quả >0

*/

public int execNoneQuery(String strSql)

{

int rec=0;

try

{

Statement st=m_Connection.createStatement();

rec=st.executeUpdate(strSql);

}

catch(SQLException ex)

{

ex.printStackTrace();

}

return rec;

}

/** Hàm này dùng đóng kết nối,

* Connection con: đối tượng connnection

*/

public void close(Connection con)

{

try

{

if(con!=null)

con.close();

}

catch(SQLException ex)

{

ex.printStackTrace();

}

}

/** Hàm này dùng đóng kết nối,

*/

public void close()

{

try

{

if(m_Connection!=null)

m_Connection.close();

}

catch(SQLException ex)

{

ex.printStackTrace();

}

}

}

class CMsAccessConnector

package tranduythanh.com.connector;import java.sql.Connection;

import java.sql.DriverManager;

public class CMsAccessConnector extends CConnector

{

/**

* Hàm dùng để kết nối tới MS Access

* strDatabase: tên CSDL

* Trả về Connection

**/

public Connection getConnect(String strServer,String strDatabase,String strUser,String strPwd)

{

try

{

String strConnect=”jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=”+strDatabase;

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

m_Connection= DriverManager.getConnection(strConnect);

}

catch(Exception e)

{

e.printStackTrace();

}

return m_Connection;

}

}

class CMySqlConnector

package tranduythanh.com.connector;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;

import com.mysql.jdbc.Driver;

public class CMySqlConnector  extends CConnector

{

/**

* Hàm dùng để kết nối tới MySQL

* strDatabase: tên CSDL

* Trả về Connection

**/

public Connection getConnect(String strServer,String strDatabase,String strUser,String strPwd)

{

String strConnect=”jdbc:mysql://”+strServer+”/”+strDatabase;

Properties pro=new Properties();

pro.put(“user”, strUser);

pro.put(“password”, strPwd);

try

{

com.mysql.jdbc.Driver driver=new Driver();

m_Connection=driver.connect(strConnect, pro);

}

catch(SQLException ex)

{

ex.printStackTrace();

}

return m_Connection;

}

}

class CSqlServerConnector

package tranduythanh.com.connector;import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class CSqlServerConnector extends CConnector

{

/**

* Hàm dùng để kết nối tới SQL Server

* strDatabase: tên CSDL

* Trả về Connection

**/

public Connection getConnect(String strServer,String strDatabase,String strUser,String strPwd)

{

try {

Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);

String connectionUrl = “jdbc:sqlserver://”+strServer+”;” +

“databaseName=”+strDatabase+”;user=”+strUser+”;password=”+strPwd+”;”;

System.out.println(“Successful”);

m_Connection= DriverManager.getConnection(connectionUrl);

System.out.println(“Successful”);

} catch (SQLException e) {

System.out.println(“SQL Exception: “+ e.toString());

} catch (ClassNotFoundException cE) {

System.out.println(“Class Not Found Exception: “+ cE.toString());

}

return m_Connection;

}

}

=================================================================

Package: tranduythanh.com.model

=================================================================

class Lophoc

package tranduythanh.com.model;import java.util.ArrayList;

public class Lophoc {

private String malop;

private String tenlop;

private int siso;

private ArrayList<Sinhvien> list=new ArrayList<Sinhvien>();

/**

* @return the list

*/

public ArrayList<Sinhvien> getList() {

return list;

}

public boolean addSinhvien(Sinhvien sv)

{

return list.add(sv);

}

public Sinhvien findSinhvienById(String masinhvien)

{

for(Sinhvien s: list)

{

if(s.getMasinhvien().equalsIgnoreCase(masinhvien))

return s;

}

return null;

}

public void removeSinhvien(String masinhvien)

{

Sinhvien sv=findSinhvienById(masinhvien);

list.remove(sv);

}

/**

* @param list the list to set

*/

public void setList(ArrayList<Sinhvien> list) {

this.list = list;

}

public Lophoc(String malop, String tenlop, int siso) {

super();

this.malop = malop;

this.tenlop = tenlop;

this.siso = siso;

}

public Lophoc() {

super();

}

/**

* @return the malop

*/

public String getMalop() {

return malop;

}

/**

* @param malop the malop to set

*/

public void setMalop(String malop) {

this.malop = malop;

}

/**

* @return the tenlop

*/

public String getTenlop() {

return tenlop;

}

/**

* @param tenlop the tenlop to set

*/

public void setTenlop(String tenlop) {

this.tenlop = tenlop;

}

/**

* @return the siso

*/

public int getSiso() {

return siso;

}

/**

* @param siso the siso to set

*/

public void setSiso(int siso) {

this.siso = siso;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return this.tenlop +” (“+this.siso+”)”;

}

}

class Sinhvien

package tranduythanh.com.model;import java.sql.Date;

public class Sinhvien {

private String masinhvien;

private String malop;

private String hoten;

private Date namsinh;

private String diachi;

/**

* @return the masinhvien

*/

public String getMasinhvien() {

return masinhvien;

}

/**

* @param masinhvien the masinhvien to set

*/

public void setMasinhvien(String masinhvien) {

this.masinhvien = masinhvien;

}

/**

* @return the malop

*/

public String getMalop() {

return malop;

}

/**

* @param malop the malop to set

*/

public void setMalop(String malop) {

this.malop = malop;

}

/**

* @return the hoten

*/

public String getHoten() {

return hoten;

}

/**

* @param hoten the hoten to set

*/

public void setHoten(String hoten) {

this.hoten = hoten;

}

/**

* @return the namsinh

*/

public Date getNamsinh() {

return namsinh;

}

/**

* @param namsinh the namsinh to set

*/

public void setNamsinh(Date namsinh) {

this.namsinh = namsinh;

}

/**

* @return the diachi

*/

public String getDiachi() {

return diachi;

}

/**

* @param diachi the diachi to set

*/

public void setDiachi(String diachi) {

this.diachi = diachi;

}

public Sinhvien(String masinhvien, String malop, String hoten,

Date namsinh, String diachi) {

super();

this.masinhvien = masinhvien;

this.malop = malop;

this.hoten = hoten;

this.namsinh = namsinh;

this.diachi = diachi;

}

public Sinhvien() {

super();

}

public Sinhvien(String masinhvien, String hoten, Date namsinh, String diachi) {

super();

this.masinhvien = masinhvien;

this.hoten = hoten;

this.namsinh = namsinh;

this.diachi = diachi;

}

}

=================================================================

Package: tranduythanh.com.ui

=================================================================

class AbstractUI

package tranduythanh.com.ui;importjava.awt.Component;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public abstract class AbstractUI extends JFrame {

private static final long serialVersionUID = 1L;

public AbstractUI(String title)

{

super(title);

}

public AbstractUI()

{

super(“Default …”);

}

public void doShow()

{

addControl();

addEventForAllControl();

doOwnerWindowFeature();

setVisible(true);

}

private void processJpanel(JPanel pn)

{

for(Component cp : pn.getComponents())

{

if(cp instanceof JButton)

{

((JButton) cp).addActionListener(new ProcessEvent());

}

else if(cp instanceof JPanel)

{

processJpanel((JPanel)cp);

}

}

}

public void addEventForAllControl()

{

Container con=getContentPane();

for(Component cp: con.getComponents())

{

if(cp instanceof JPanel)

{

processJpanel((JPanel)cp);

}

}

}

public abstract void addControl();

public abstract void doActionControl(Object o);

public abstract void doOwnerWindowFeature();

private class ProcessEvent implements ActionListener

{

@Override

public void actionPerformed(ActionEvent arg0) {

doActionControl(arg0.getSource());

}

}

}

class AbstractConnectUI

package tranduythanh.com.ui;importjava.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.util.ArrayList;

import java.util.Vector;

import javax.swing.BorderFactory;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JSplitPane;

import javax.swing.JTable;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.border.TitledBorder;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

import javax.swing.table.DefaultTableModel;

import tranduythanh.com.connector.CConnector;

import tranduythanh.com.model.Lophoc;

import tranduythanh.com.model.Sinhvien;

public abstract class  AbstractConnectUI extends AbstractUI{

/**

*

*/

private static final long serialVersionUID = 1L;

protected static JList lstData;

protected JTable tblData;

protected DefaultTableModel dtmData;

protected JButton btnXoaLop,btnThemLop,btnSuaLop,btnThemSinhvien,btnLuuSinhvien,btnXoaSinhvien;

protected JTextField txtMasinhvien,txtTenSinhvien,txtNgaysinh,txtThangsinh,txtNamsinh;

protected JTextArea txtDiachi;

protected static JComboBox cboDanhsachlop;

protected static ArrayList<Lophoc> listLophoc;

protected static ArrayList<Sinhvien> listSinhvien;

protected static Lophoc lophocSelected;

protected static Sinhvien sinhvienSelected;

protected CConnector m_connector;

public AbstractConnectUI(String title)

{

super(title);

}

public AbstractConnectUI()

{

super(“Default …”);

}

@Override

public void addControl() {

// TODO Auto-generated method stub

JPanel pnBorder=new JPanel();

pnBorder.setLayout(new BorderLayout());

JPanel pnNorth=new JPanel();

JLabel lblTitle=new JLabel(“Quản lý Sinh Viên – Lớp Học”);

Font ftTitle=new Font(“arial”, Font.BOLD, 32);

lblTitle.setFont(ftTitle);

lblTitle.setForeground(Color.BLUE);

pnNorth.add(lblTitle);

pnBorder.add(pnNorth,BorderLayout.NORTH);

JPanel pnListLop=new JPanel();

JPanel pnListSinhvien=new JPanel();

JSplitPane slitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pnListLop, pnListSinhvien);

pnBorder.add(slitPane,BorderLayout.CENTER);

pnListLop.setLayout(new BorderLayout());

lstData=new JList();

TitledBorder cateborder=new TitledBorder(BorderFactory.createLineBorder(Color.RED), “Danh sách lớp học”);

pnListLop.setBorder(cateborder);

pnListLop.setPreferredSize(new Dimension(300, 0));

pnListLop.add(lstData,BorderLayout.CENTER);

JPanel pnListLopSouth=new JPanel();

btnThemLop =new JButton(“Thêm lớp”);

pnListLopSouth.add(btnThemLop);

btnSuaLop =new JButton(“Sửa lớp”);

pnListLopSouth.add(btnSuaLop);

btnXoaLop =new JButton(“Xóa lớp”);

pnListLopSouth.add(btnXoaLop);

pnListLop.add(pnListLopSouth,BorderLayout.SOUTH);

pnListSinhvien.setLayout(new BorderLayout());

JPanel pnSinhvienTitle=new JPanel();

JLabel lblSinhvienTitle=new JLabel(“Thông tin chi tiết”);

pnSinhvienTitle.add(lblSinhvienTitle);

pnListSinhvien.add(pnSinhvienTitle,BorderLayout.NORTH);

JPanel pnSinhvienTable=new JPanel();

pnSinhvienTable.setLayout(new BorderLayout());

pnListSinhvien.add(pnSinhvienTable,BorderLayout.CENTER);

dtmData =new DefaultTableModel();

dtmData.addColumn(“Mã Sinh Viên”);

dtmData.addColumn(“Tên Sinh Viên”);

dtmData.addColumn(“Ngày Sinh”);

dtmData.addColumn(“Lớp”);

dtmData.addColumn(“Địa chỉ”);

tblData=new JTable(dtmData);

JScrollPane sctblsinhvien=new JScrollPane(tblData);

pnSinhvienTable.add(sctblsinhvien,BorderLayout.CENTER);

JPanel pnSinhvienDetail=new JPanel();

pnListSinhvien.add(pnSinhvienDetail,BorderLayout.SOUTH);

pnSinhvienDetail.setLayout(new BoxLayout(pnSinhvienDetail, BoxLayout.Y_AXIS ));

JPanel pnLopList=new JPanel();

JLabel lblLopId=new JLabel(“Lớp học :”);

cboDanhsachlop=new JComboBox();

pnLopList.add(lblLopId);

pnLopList.add(cboDanhsachlop);

pnSinhvienDetail.add(pnLopList);

JPanel pnSinhvienId=new JPanel();

JLabel lblSinhvienId=new JLabel(“Mã sinh viên:”);

txtMasinhvien=new JTextField(20);

pnSinhvienId.add(lblSinhvienId);

pnSinhvienId.add(txtMasinhvien);

pnSinhvienDetail.add(pnSinhvienId);

JPanel pnSinhvienName=new JPanel();

JLabel lblSinhvienName=new JLabel(“Tên sinh viên:”);

txtTenSinhvien=new JTextField(20);

pnSinhvienName.add(lblSinhvienName);

pnSinhvienName.add(txtTenSinhvien);

pnSinhvienDetail.add(pnSinhvienName);

JPanel pnNgaysinh=new JPanel();

JLabel lblNgaysinh=new JLabel(“Ngày Sinh: “);

txtNgaysinh=new JTextField(3);

txtThangsinh=new JTextField(3);

txtNamsinh=new JTextField(4);

pnNgaysinh.add(lblNgaysinh);

JPanel pnNgaySinhChitiet=new JPanel();

pnNgaySinhChitiet.setLayout(new FlowLayout(FlowLayout.LEFT));

pnNgaySinhChitiet.add(txtNgaysinh);

pnNgaySinhChitiet.add(txtThangsinh);

pnNgaySinhChitiet.add(txtNamsinh);

JLabel lblNtnsFormat=new JLabel(“(dd-mm-yyyy)”);

lblNtnsFormat.setFont(new Font(“arial”,Font.ITALIC,10));

pnNgaySinhChitiet.add(lblNtnsFormat);

pnNgaysinh.add(pnNgaySinhChitiet);

pnSinhvienDetail.add(pnNgaysinh);

JPanel pnSinhvienDescription=new JPanel();

JLabel lblDescription=new JLabel(“Địa chỉ:”);

txtDiachi=new JTextArea(4, 20);

JScrollPane scare=new JScrollPane(txtDiachi);

pnSinhvienDescription.add(lblDescription);

pnSinhvienDescription.add(scare);

pnSinhvienDetail.add(pnSinhvienDescription);

JPanel pnButton=new JPanel();

btnThemSinhvien=new JButton(“Thêm SV”);

btnLuuSinhvien=new JButton(“Lưu SV”);

btnXoaSinhvien=new JButton(“Xóa SV”);

pnButton.add(btnThemSinhvien);

pnButton.add(btnLuuSinhvien);

pnButton.add(btnXoaSinhvien);

pnSinhvienDetail.add(pnButton);

cboDanhsachlop.setPreferredSize(txtMasinhvien.getPreferredSize());

lblLopId.setPreferredSize(lblSinhvienName.getPreferredSize());

lblDescription.setPreferredSize(lblSinhvienName.getPreferredSize());

lblNgaysinh.setPreferredSize(lblSinhvienName.getPreferredSize());

lblSinhvienId.setPreferredSize(lblSinhvienName.getPreferredSize());

Container con=getContentPane();

con.add(pnBorder);

lstData.addListSelectionListener(new ProcessEvent());

tblData.addMouseListener(new ProcessEvent());

}

@Override

public void doActionControl(Object o) {

// TODO Auto-generated method stub

if(o.equals(lstData))

{

showLopAndSinhvien();

}

else if(o.equals(tblData))

{

showChitietSinhvien();

}

}

private class ProcessEvent implements ListSelectionListener,MouseListener

{

@Override

public void valueChanged(ListSelectionEvent arg0) {

// TODO Auto-generated method stub

doActionControl(arg0.getSource());

}

@Override

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

doActionControl(e.getSource());

}

@Override

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

}

}

@Override

public void doOwnerWindowFeature() {

// TODO Auto-generated method stub

setSize(800, 600);

setLocationRelativeTo(null);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setResizable(false);

}

public abstract ArrayList<Lophoc> getListLop();

public abstract ArrayList<Sinhvien> getListSinhvienByIdLop(String malop);

public void showLopAndSinhvien()

{

Lophoc lh=(Lophoc ) lstData.getSelectedValue();

listSinhvien= getListSinhvienByIdLop(lh.getMalop());

lh.setList(listSinhvien);

lophocSelected=lh;

showListSinhvienIntoTable();

}

public void showListSinhvienIntoTable()

{

dtmData.setRowCount(0);

for(Sinhvien s: lophocSelected.getList())

{

Vector<String> vec=new Vector<String>();

vec.add(s.getMasinhvien());

vec.add(s.getHoten());

vec.add(s.getNamsinh().getDate()+”/”+(s.getNamsinh().getMonth()+1)+”/”+(s.getNamsinh().getYear()+1900));

vec.add(s.getMalop());

vec.add(s.getDiachi());

dtmData.addRow(vec);

}

}

public void showChitietSinhvien()

{

int row=tblData.getSelectedRow();

sinhvienSelected =listSinhvien.get(row);

txtMasinhvien.setText(sinhvienSelected.getMasinhvien());

txtTenSinhvien.setText(sinhvienSelected.getHoten());

txtDiachi.setText(sinhvienSelected.getDiachi());

txtNgaysinh.setText(sinhvienSelected.getNamsinh().getDate()+””);

txtThangsinh.setText((sinhvienSelected.getNamsinh().getMonth()+1)+””);

txtNamsinh.setText((sinhvienSelected.getNamsinh().getYear()+1900)+””);

cboDanhsachlop.setSelectedItem(lophocSelected);

}

public void updateLopList()

{

lstData.removeAll();

ArrayList<Lophoc> listlop=getListLop();

lstData.setListData(listlop.toArray());

lstData.updateUI();

cboDanhsachlop.removeAllItems();

for(Lophoc lh : listlop)

{

cboDanhsachlop.addItem(lh);

}

}

public void doComboboxSelected()

{

lophocSelected=(Lophoc) cboDanhsachlop.getSelectedItem();

}

}

class MainUI

package tranduythanh.com.ui;importjava.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import javax.swing.BorderFactory;

import javax.swing.BoxLayout;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.border.TitledBorder;

public class MainUI extends AbstractUI{

private static final long serialVersionUID = 1L;

JButton btnMsAcces,btnSqlServer,btnMysql,btnShutdown;

public MainUI(String title)

{

super(title);

}

public void addControl()

{

JPanel pnButton=new JPanel();

TitledBorder bor1=new TitledBorder(BorderFactory.createEtchedBorder(Color.RED, Color.BLUE), “Chọn loại kết nối”);

pnButton.setBorder(bor1);

JPanel pnAccess=new JPanel();

pnAccess.setLayout(new BoxLayout(pnAccess, BoxLayout.Y_AXIS));

ImageIcon icon=new ImageIcon(“images/ms-access.png”);

btnMsAcces=new JButton();

btnMsAcces.setIcon(icon);

pnAccess.add(btnMsAcces);

JLabel lblAccess=new JLabel(“Ms Access”);

pnAccess.add(lblAccess);

pnButton.add(pnAccess);

JPanel pnSqlServer=new JPanel();

pnSqlServer.setLayout(new BoxLayout(pnSqlServer, BoxLayout.Y_AXIS));

icon=new ImageIcon(“images/sqlserver.png”);

btnSqlServer=new JButton();

btnSqlServer.setIcon(icon);

pnSqlServer.add(btnSqlServer);

JLabel lblSqlServer=new JLabel(“Sql Server”);

pnSqlServer.add(lblSqlServer);

pnButton.add(pnSqlServer);

JPanel pnMysql=new JPanel();

pnMysql.setLayout(new BoxLayout(pnMysql, BoxLayout.Y_AXIS));

icon=new ImageIcon(“images/mysql.png”);

btnMysql=new JButton();

btnMysql.setIcon(icon);

pnMysql.add(btnMysql);

JLabel lblMysql=new JLabel(“My Sql”);

pnMysql.add(lblMysql);

pnButton.add(pnMysql);

Container con=getContentPane();

con.setLayout(new BorderLayout());

con.add(pnButton,BorderLayout.CENTER);

btnShutdown=new JButton();

icon=new ImageIcon(“images/shutdown.png”);

btnShutdown.setIcon(icon);

JPanel pnShutdown=new JPanel();

pnShutdown.setBackground(Color.PINK);

pnShutdown.add(btnShutdown);

TitledBorder bor2=new TitledBorder(BorderFactory.createEtchedBorder(Color.RED, Color.BLUE), “Thoát khỏi chương trình”);

bor2.setTitleColor(Color.BLUE);

bor2.setTitleJustification(TitledBorder.RIGHT);

pnShutdown.setBorder(bor2);

con.add(pnShutdown,BorderLayout.SOUTH);

btnMsAcces.setToolTipText(“Click vào đây để kết nối tới CSDL MS Access!”);

btnSqlServer.setToolTipText(“Click vào đây để kết nối tới CSDL SQL Server!”);

btnMysql.setToolTipText(“Click vào đây để kết nối tới CSDL My SQL!”);

btnShutdown.setToolTipText(“Click vào đây để thoát chương trình!”);

}

@Override

public void doActionControl(Object o) {

if(o.equals(btnMsAcces))

{

JOptionPane.showMessageDialog(null, “MS Access”);

ConnectMsAccessUI msAccessUI=new ConnectMsAccessUI(“MS Access Demo”);

msAccessUI.doShow();

}

else if(o.equals(btnMysql))

{

JOptionPane.showMessageDialog(null, “MySQL”);

ConnectMySqlUI mysqlUI=new ConnectMySqlUI(“MySQl Demo”);

mysqlUI.doShow();

}

else if(o.equals(btnSqlServer))

{

JOptionPane.showMessageDialog(null, “MS SqlServer”);

ConnectSqlServerUI ui=new ConnectSqlServerUI(“SQL server”);

ui.doShow();

}

else if(o.equals(btnShutdown))

{

int ret=JOptionPane.showConfirmDialog(this, “Muốnt thoát hả?”,”Thoát”,JOptionPane.YES_NO_OPTION);

if(ret==JOptionPane.YES_OPTION)

{

System.exit(0);

}

}

}

public static void main(String[] args) {

MainUI ui=new MainUI(“Demo Kết nối nhiều CSDL”);

ui.doShow();

}

@Override

public void doOwnerWindowFeature() {

// TODO Auto-generated method stub

setSize(400, 250);

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setResizable(false);

}

}

class ConnectMsAccessUI

package tranduythanh.com.ui;importjava.sql.ResultSet;

import java.util.ArrayList;

import tranduythanh.com.connector.CMsAccessConnector;

import tranduythanh.com.model.Lophoc;

import tranduythanh.com.model.Sinhvien;

public class ConnectMsAccessUI extends AbstractConnectUI

{

/**

*

*/

private static final long serialVersionUID = 1L;

public ConnectMsAccessUI(String title)

{

super(title);

m_connector=new CMsAccessConnector();

}

@Override

public void addControl() {

// TODO Auto-generated method stub

super.addControl();

updateLopList();

}

@Override

public void doActionControl(Object o) {

// TODO Auto-generated method stub

super.doActionControl(o);

}

@Override

public void doOwnerWindowFeature() {

super.doOwnerWindowFeature();

}

@Override

public ArrayList<Lophoc> getListLop() {

// TODO Auto-generated method stub

ArrayList<Lophoc>listLop=new ArrayList<Lophoc>();

m_connector.getConnect(“”, “database\\quanlysinhvien.accdb”, “”, “”);

ResultSet rs= m_connector.execQuery(“select * from lophoc”);

try

{

while(rs.next())

{

Lophoc lh=new Lophoc(rs.getString(1), rs.getString(2), rs.getInt(3));

listLop.add(lh);

}

}

catch(Exception ex)

{

ex.printStackTrace();

}

return listLop;

}

@Override

public ArrayList<Sinhvien> getListSinhvienByIdLop(String malop) {

// TODO Auto-generated method stub

ArrayList<Sinhvien>listSv=new ArrayList<Sinhvien>();

m_connector.getConnect(“”, “database\\quanlysinhvien.accdb”, “”, “”);

ResultSet rs= m_connector.execQuery(“select * from sinhvien where malop='”+malop+”‘”);

try

{

while(rs.next())

{

Sinhvien sv=new Sinhvien(rs.getString(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getString(5));

listSv.add(sv);

}

}

catch(Exception ex)

{

ex.printStackTrace();

}

return listSv;

}

}

class ConnectMySqlUI

package tranduythanh.com.ui;importjava.sql.ResultSet;

import java.util.ArrayList;

import tranduythanh.com.connector.CMySqlConnector;

import tranduythanh.com.model.Lophoc;

import tranduythanh.com.model.Sinhvien;

public class ConnectMySqlUI extends AbstractConnectUI{

private static final long serialVersionUID = 1L;

public ConnectMySqlUI(String title)

{

super(title);

CMySqlConnector con=new CMySqlConnector();

con.getConnect(“localhost”, “dbtest”, “root”, “hoilamgi”);

ResultSet rs= con.execQuery(“select * from tblemployee”);

try

{

while(rs.next())

{

System.out.println(rs.getInt(1) +” – “+rs.getString(2));

}

}

catch(Exception ex)

{

}

}

@Override

public void addControl() {

// TODO Auto-generated method stub

super.addControl();

}

@Override

public void doActionControl(Object o) {

// TODO Auto-generated method stub

}

@Override

public void doOwnerWindowFeature() {

// TODO Auto-generated method stub

super.doOwnerWindowFeature();

}

@Override

public ArrayList<Lophoc> getListLop() {

// TODO Auto-generated method stub

return null;

}

@Override

public ArrayList<Sinhvien> getListSinhvienByIdLop(String malop) {

// TODO Auto-generated method stub

return null;

}

}

class ConnectSqlServerUI

package tranduythanh.com.ui;importjava.sql.ResultSet;

importjava.util.ArrayList;

import tranduythanh.com.connector.CSqlServerConnector;

import tranduythanh.com.model.Lophoc;

import tranduythanh.com.model.Sinhvien;

public class ConnectSqlServerUI extends AbstractConnectUI{

/**

*

*/

private static final long serialVersionUID = 1L;

public ConnectSqlServerUI(String title)

{

CSqlServerConnector con=new CSqlServerConnector();

con.getConnect(“fithui”, “dbexample”, “sa”, “hoilamgi”);

ResultSet rs=con.execQuery(“select * from tblCustomer”);

try

{

while(rs.next())

{

System.out.println(rs.getString(2)+” – “+ rs.getString(4));

}

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

@Override

public void addControl() {

// TODO Auto-generated method stub

super.addControl();

}

@Override

public void doActionControl(Object o) {

// TODO Auto-generated method stub

}

@Override

public void doOwnerWindowFeature() {

super.doOwnerWindowFeature();

}

@Override

public ArrayList<Lophoc> getListLop() {

// TODO Auto-generated method stub

return null;

}

@Override

public ArrayList<Sinhvien> getListSinhvienByIdLop(String malop) {

// TODO Auto-generated method stub

return null;

}

}

Các bạn ráng cài đặt để chạy được chương trình.

Chúc các bạn thành công.

Coding mẫu đầy đủ: http://www.mediafire.com/download.php?thpatqs1f0lpn2n (tham khảo chứ đừng copy paste code)

Link download thư viện kết nối tới MySQL: http://www.mediafire.com/?6cz23q7fmhmq1a1

Link download thư viện kết nối tới SQL Server: http://www.mediafire.com/?eiw554aacw2h4j9