03. Cách kết nối với cơ sở dữ liệu bằng JDBC



Bản quyền thuộc về TITV.vn, 
vui lòng không đăng tải lại nội dung từ trang này.

Video giải thích chi tiết


 Nội dung chi tiết 


Bài 3. Giới thiệu về JDBC và cách kết nối với CSDL


Giới thiệu JDBC


Java Database Connectivity là một API được thiết kế dành cho ngôn ngữ lập trình Java hỗ trợ Java trong việc truy cập Cơ Sở Dữ Liệu. Nó gồm có những phương thức thực hiện truy vấn và cập nhật CSDL gián tiếp qua Java.


JDBC Driver



https://www.java67.com/2015/07/difference-between-type-1-2-3-and-4-jdbc-drivers-java.html#:~:text=The%20difference%20between%20different%20types%20of%20JDBC%20drivers%20comes%20from,and%20gives%20the%20best%20performance.




Cách kết nối với CSDL

a, Thông số cơ bản


MySQL Database

DBMS: MySQL Server 5.0

Vendor: Oracle Corporation

Web Site: http://www.oracle.com

Driver Type: JDBC Driver (Pure Java)

URL Format: jdbc:mySQL://<server>:<port>/<database>?<props> 

URL Example: jdbc:mySQL://HYE6754:3306/chanda

URL Example: jdbc:mySQL://HYE6754:3306/chanda

Driver Class: com.mySQL.jdbc.Driver

Oracle Database

DBMS: Oracle 10g Vendor: Oracle Corporation

Web Site: http://www.oracle.com 

Driver Type: JDBC Driver (thin - Pure Java) 

URL Format: jdbc:oracle:thin:@::<server>:<port>:<database>:<props>  

URL Example: jdbc:oracle:thin:@localhost:1521:chanda Driver Class: oracle.jdbc.driver.OracleDriver

SQL Server

// Driver #1

DBMS: SQL Server

Vendor: Microsoft Corporation

Web Site: http://www.microsoft.com

Driver Type: JDBC Driver (Pure Java)

URL Format: jdbc:SQLserver://<server>:<port>

URL Example: jdbc:SQLserver://HYE6754:1433;Database=chanda

Driver Class: com.microsoft.SQLserver.jdbc.SQLServerDriver


// Driver #2

DBMS: SQL Server

Vendor: SourceForge Inc.

Web Site: http://www.sourceforge.net

Driver Type: JDBC Driver (Pure Java)

URL Format: jdbc:jtds:<server_type>://<server>:<port>/<database>;<props>

URL Example: jdbc:jtds:sqlserver://HYE6754:1433/chanda

Driver Class: net.sourceforge.jtds.jdbc.Driver


b, Cách kết nối

Các bước:


Bước 1: Tải về trình điều khiển JDBC và thêm nó vào đường dẫn mô-đun khi bạn chạy Java của mình các chương trình.


https://jar-download.com/artifacts/mysql/mysql-connector-java


https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15



Bước 2: Trong JDK8 trở về trước, cần đăng ký trình điều khiển JDBC với DriverManager. Từ JDK9, bạn không cần thực hiện bước này. Nếu bạn đã đặt JAR hoặc JAR mô-đun cho JDBC của mình trình điều khiển trên đường dẫn mô-đun, cơ chế nhà cung cấp dịch vụ trong JDK9 sẽ đăng ký trình điều khiển JDBC tự động cho bạn.


// Register the Oracle JDBC driver with DriverManager 

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 


// Register the Apache Derby embedded driver 

DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver()); 


// Register the Apache Derby network client driver 

DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());



Bước 3: Xây dựng một URL kết nối.

Cần có:

Link của Database

Port

Database

Username

Password


jdbc:mySQL://localhost:3306/chanda?user=app&password=app

jdbc:db2://localhost:5021/chandaDB:user=admin;password=secret;



Bước 4: Sử dụng phương thức tĩnh getConnection () của DriverManager để thiết lập kết nối.


Connection getConnection(String url) throws SQLException 

Connection getConnection(String url, Properties info) throws SQLException

Connection getConnection(String url, String user, String password) throws SQLException





package database;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.cj.jdbc.Driver;

public class JDBCUtil {
	public static Connection getConnection() {
		Connection c = null;
		
		try {
			// Đăng ký MySQL Driver với DriverManager
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			
			// Các thông số
			String url = "jdbc:mySQL://localhost:3306/ontap";
			String username = "root";
			String password = "";
			
			// Tạo kết nối
			c = DriverManager.getConnection(url, username, password);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return c;
	}
	
	public static void closeConnection(Connection c) {
		try {
			if(c!=null) {
				c.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void printInfo(Connection c) {
		try {
			if(c!=null) {
				DatabaseMetaData mtdt = c.getMetaData();
				System.out.println(mtdt.getDatabaseProductName());
				System.out.println(mtdt.getDatabaseProductVersion());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

    

package test;

import java.sql.Connection;

import database.JDBCUtil;

public class TestJDBCUtil {
	public static void main(String[] args) {
		Connection connection = JDBCUtil.getConnection();
		
		JDBCUtil.printInfo(connection);
		
		JDBCUtil.closeConnection(connection);
		
	}
	
	
}


    

Không có nhận xét nào:

Đăng nhận xét