Trong bài viết này, bạn sẽ được học cách kết nối MySQL bằng cách sử dụng thư viện Java JDBC Connection, đây là bước đầu tiên và quan trọng nhất khi bạn muốn xử lý cơ sở dữ liệu trong Java JDBC
Bài viết này đã được đăng tại freetuts. net , không được sao chép dưới mọi hình thức.
Để kết nối với MySQL từ một chương trình Java, bạn cần thực hiện các bước sau
- Tải thư viện MySQL Connector/J vào trong dự án của bạn
- Tạo một đối tượng mới từ lớp
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }
7, sau đó bạn có thể thực hiện kết nối
1. Tải thư viện MySQL Connector/J
Để tải thư viện MySQL
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }8, bạn thực hiện các bước sau
Bước 1. Tại Netbeans IDE, bạn hãy tạo một dự án, sau đó nhấp chuột phải và chọn
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }9 từ menu xổ số. Như trong hình dưới đây mình đã đặt tên là
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }0
Bài viết này đã được đăng tại [free tuts. mạng lưới]
Bước 2. Ở phía bên tay trái, bạn hãy chọn vào
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }0, sau đó nhấp vào nút
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }1
Bước 3. Một hộp thoại xuất hiện, bạn hãy tìm đến thư mục cài đặt MySQL Connector/J mà bạn đã thực hiện ở bài viết trước
2. MySQL connection with Java JDBC Connection
Đầu tiên bạn hãy nhập ba lớp quan trọng nhất vào dự án, đó là
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }2,
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }7, và
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }4 từ
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }5
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;
Tiếp theo hãy gọi phương thức
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }6 từ lớp
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }7 để khởi tạo Đối tượng kết nối
Will have ba tham số bạn cần truyền vào phương thức này
- url. This is database connection string. Đối với MySQL thì nó có dạng như sau
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }
8, tức là bạn đang kết nối với máy chủ localhost, cổng 3306 và tên cơ sở dữ liệu làConnection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }
9 - người sử dụng. Tên đăng nhập của người dùng kết nối vào cơ sở dữ liệu
- mật khẩu mở khóa. Mật khẩu của người dùng
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }
If too too login not public by a number of reason as. Mật khẩu sai, tên đăng nhập sai, cổng kết nối không tồn tại, cơ sở dữ liệu không tồn tại, lúc này sẽ phát sinh lỗi SQLException. Vì vậy khi kết nối cơ sở dữ liệu bạn nên đặt nó trong khối
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }00 để đảm bảo an toàn
Ngoài ra, sau khi sử dụng xong, bạn nên tắt kết nối ngay để tránh bị tấn công vào dữ liệu. Để tắt kết nối đơn giản hơn, bạn chỉ cần gọi phương thức close[] trong Đối tượng kết nối mà bạn đã tạo ra
Từ Java 7 trở lên có một câu lệnh hay cách gọi khác là
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }01 cho phép bạn đơn giản hóa mã hóa ở trên như sau
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }0
Nó tự động gọi phương thức
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }02 của các đối tượng kết nối sau khi chương trình kết thúc. Như bạn có thể thấy chương trình trở nên rõ ràng hơn nhiều
Tuy nhiên, thực tế là bạn không nên sử dụng cách này, bởi vì nó sẽ gặp một số vấn đề như. Khi bạn thay đổi cơ sở dữ liệu hoặc thông số kết nối thì phải sửa khá nhiều vị trí, đồng thời không được bảo mật khi mã nguồn có quá nhiều nơi kết nối
Để tránh tạo mã kết nối phần cứng như vậy, ta có một giải pháp khá hay, đó là tạo ra một tệp chứa các số kết nối này. Lúc này nếu thay đổi gì thì chỉ cần vào tập tin này là có thể thực hiện được
Mình tạo tên file là db. properties with content as after
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }4
Để sử dụng nhưng thuộc tính này thì mình sẽ viết lại đoạn mã kết nối như sau
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }5
Với mỗi lần tương tác với MySQL, bạn sẽ tạo ra một két nối mới, điều này làm cho chương trình tốn quá nhiều bộ nhớ để lưu trữ nhưng đối tượng kết nối đó
Vì vậy ta nên tạo ra một lớp riêng và nơi nào muốn sử dụng thì chỉ cần gọi đến là được
Connection conn = null; try { // db parameters String url = "jdbc:mysql://localhost:3306/mysqljdbc"; String user = "root"; String password = "secret"; // create a connection to the database conn = DriverManager.getConnection[url, user, password]; // more processing here // ... } catch[SQLException e] { System.out.println[e.getMessage[]]; } finally { try{ if[conn ! null] conn.close[] }catch[SQLException ex]{ System.out.println[ex.getMessage[]] } }6
Như vậy là trong bài này mình đã hướng dẫn xong cách kết nối với MySQL trong Java JDBC Driver. Chúc bạn thực hiện thành công và gặp lại bạn ở những bài tiếp theo nhé