Electrical Resistor,3C Electronics,Electronic Surplus Blog - grbelectrical.com

Jdbc three ways to register drivers

About the JDBC Driver Package

To use a JDBC driver, you first need to import the corresponding package, such as mysql-connector-java-5.0.8-bin.jar. The driver acts as a bridge between Java and a specific database, developed by the database vendor. Each database has its own driver JAR file, and even different versions of the same database may require specific driver versions.

1: First Method

Class.forName("oracle.jdbc.driver.OracleDriver"); // Load the driver

Connection conn = DriverManager.getConnection(

"jdbc:oracle:thin:@127.0.0.1:1521:XE", "system", "123456" );

System.out.println("conn============>" + conn);

JDBC Three Ways to Register Drivers

2: Second Method

Driver driver = new oracle.jdbc.driver.OracleDriver();

DriverManager.registerDriver(driver); // (This step is optional in JDBC 2.0 and later, as it's done automatically)

Connection conn = DriverManager.getConnection(

"jdbc:oracle:thin:@127.0.0.1:1521:XE", "system", "123456" );

System.out.println("conn============>" + conn);

JDBC Three Ways to Register Drivers

3: Third Method

Add the full path of the Oracle driver (ojdbc.jar) to the classpath. You can also set the system property to specify the driver class directly. For example, using the command line:

java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver

This method allows you to register multiple drivers at once by separating them with colons. For instance:

System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver:oracle.jdbc.driver.OracleDriver");

This will register both the MySQL and Oracle drivers simultaneously.

After reviewing the DriverManager source code, it becomes clear that when you call getConnection(), it automatically loads all drivers specified in the jdbc.drivers system property. This makes the third method especially useful when working with multiple databases or environments.

80W Medical Power Supply

80W Medical Power Supply,80W Medical Device Power Supply,80W Medical Power Adapter,80W Rade Power Supplies

Shenzhen Longxc Power Supply Co., Ltd , https://www.longxcpower.com