Trước tiên, bạn cần thiết lập kết nối với nguồn dữ liệu bạn muốn sử dụng. Nguồn dữ liệu có thể là DBMS, hệ thống tệp kế thừa hoặc một số nguồn dữ liệu khác có trình điều khiển JDBC tương ứng. Thông thường, ứng dụng JDBC kết nối với nguồn dữ liệu đích bằng cách sử dụng một trong hai lớp
public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 2. Lớp được triển khai đầy đủ này kết nối ứng dụng với nguồn dữ liệu, được chỉ định bởi URL cơ sở dữ liệu. Khi lớp này cố gắng thiết lập kết nối lần đầu tiên, nó sẽ tự động tải bất kỳ JDBC 4 nào. 0 trình điều khiển được tìm thấy trong đường dẫn lớp. Lưu ý rằng ứng dụng của bạn phải tải bất kỳ trình điều khiển JDBC nào trước phiên bản 4 theo cách thủ công. 0
public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 3. Giao diện này được ưa thích hơn public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 2 vì nó cho phép các chi tiết về nguồn dữ liệu cơ bản minh bạch đối với ứng dụng của bạn. Các thuộc tính của đối tượng public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 3 được đặt sao cho nó đại diện cho một nguồn dữ liệu cụ thể. Xem Kết nối với các đối tượng DataSource để biết thêm thông tin. Để biết thêm thông tin về việc phát triển các ứng dụng với lớp public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 3, hãy xem Hướng dẫn Java EE mới nhất
Ghi chú. Các mẫu trong hướng dẫn này sử dụng lớp public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 2 thay vì lớp public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 3 vì nó dễ sử dụng hơn và các mẫu không yêu cầu các tính năng của lớp public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 3
Trang này bao gồm các chủ đề sau
Kết nối với DBMS của bạn với lớp public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 2 liên quan đến việc gọi phương thức jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]* 2. Phương pháp sau đây, jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]* 3, thiết lập kết nối cơ sở dữ liệu
public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; }
Phương pháp jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]* 2 thiết lập kết nối cơ sở dữ liệu. Phương pháp này yêu cầu một URL cơ sở dữ liệu, thay đổi tùy thuộc vào DBMS của bạn. Sau đây là một số ví dụ về URL cơ sở dữ liệu
mysql. jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]* 5, trong đó jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]* 6 là tên của máy chủ lưu trữ cơ sở dữ liệu của bạn và jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]* 7 là số cổng
Cơ sở dữ liệu Java. jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]* 8, trong đó jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]* 9 là tên của cơ sở dữ liệu để kết nối và jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 0 hướng dẫn DBMS tạo cơ sở dữ liệu
Ghi chú. URL này thiết lập kết nối cơ sở dữ liệu với Trình điều khiển nhúng Java DB. Java DB cũng bao gồm Trình điều khiển máy khách mạng, sử dụng một URL khác
Phương pháp này chỉ định tên người dùng và mật khẩu cần thiết để truy cập DBMS bằng đối tượng jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 1
Ghi chú
Thông thường, trong URL cơ sở dữ liệu, bạn cũng chỉ định tên của cơ sở dữ liệu hiện có mà bạn muốn kết nối. Ví dụ: URL jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 2 đại diện cho URL cơ sở dữ liệu cho cơ sở dữ liệu MySQL có tên jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 3. Các mẫu trong hướng dẫn này sử dụng một URL không chỉ định cơ sở dữ liệu cụ thể vì các mẫu tạo cơ sở dữ liệu mới
Trong các phiên bản trước của JDBC, để có được kết nối, trước tiên bạn phải khởi tạo trình điều khiển JDBC của mình bằng cách gọi phương thức jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 4. Phương pháp này yêu cầu một đối tượng loại jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 5. Mỗi trình điều khiển JDBC chứa một hoặc nhiều lớp cài đặt giao diện jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 5. Trình điều khiển cho Java DB là jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 7 và jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 8, và trình điều khiển cho MySQL Connector/J là jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 9. Xem tài liệu về trình điều khiển DBMS của bạn để biết tên của lớp thực hiện giao diện jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 5
Bất kỳ JDBC 4 nào. 0 trình điều khiển được tìm thấy trong đường dẫn lớp của bạn sẽ tự động được tải. (Tuy nhiên, bạn phải tải bất kỳ trình điều khiển nào theo cách thủ công trước JDBC 4. 0 với phương pháp jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]... 4. )
Phương thức này trả về một đối tượng public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 22, đại diện cho một kết nối với DBMS hoặc một cơ sở dữ liệu cụ thể. Truy vấn cơ sở dữ liệu thông qua đối tượng này
URL kết nối cơ sở dữ liệu là một chuỗi mà trình điều khiển DBMS JDBC của bạn sử dụng để kết nối với cơ sở dữ liệu. Nó có thể chứa thông tin như nơi tìm kiếm cơ sở dữ liệu, tên của cơ sở dữ liệu để kết nối và thuộc tính cấu hình. Cú pháp chính xác của URL kết nối cơ sở dữ liệu được chỉ định bởi DBMS của bạn
URL kết nối cơ sở dữ liệu Java DB
Sau đây là cú pháp URL kết nối cơ sở dữ liệu cho Java DB
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
- public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 23 chỉ định nơi Java DB sẽ tìm kiếm cơ sở dữ liệu, trong thư mục, trong bộ nhớ, trong đường dẫn lớp hoặc trong tệp JAR. Nó thường được bỏ qua
- public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; } 24 là tên của cơ sở dữ liệu để kết nối với
- public Connection getConnection() throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", this.userName);
connectionProps.put("password", this.password);
if (this.dbms.equals("mysql")) {
conn = DriverManager.getConnection(
"jdbc:" + this.dbms + "://" +
this.serverName +
":" + this.portNumber + "/",
connectionProps);
} else if (this.dbms.equals("derby")) {
conn = DriverManager.getConnection(
"jdbc:" + this.dbms + ":" +
this.dbName +
";create=true",
connectionProps);
}
System.out.println("Connected to database");
return conn;
}
25 đại diện cho một danh sách các thuộc tính tùy chọn, được phân tách bằng dấu chấm phẩy. Các thuộc tính này cho phép bạn hướng dẫn Java DB thực hiện các tác vụ khác nhau, bao gồm các tác vụ sau
- Tạo cơ sở dữ liệu được chỉ định trong URL kết nối
- Mã hóa cơ sở dữ liệu được chỉ định trong URL kết nối
- Chỉ định các thư mục để lưu trữ thông tin ghi nhật ký và theo dõi
- Chỉ định tên người dùng và mật khẩu để kết nối với cơ sở dữ liệu
Xem Hướng dẫn dành cho nhà phát triển Java DB và Hướng dẫn tham khảo Java DB từ Tài liệu kỹ thuật Java DB để biết thêm thông tin