Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

JDBC series: (1) connect to the database through JDBC

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. What is JDBC

2. Three ways for JDBC to connect to the database

2.1. The first way of implementation

2.2. The second way of implementation

2.3. The third way of implementation

3. Internal implementation of com.mysql.jdbc.Driver

1. What is JDBC

The technique of sending sql statements using java code (program)

Sending sql using jdbc requires knowing the IP address, port, data name, user name, and password of the database.

JDBC URL= protocol name + sub-protocol name + data source name. A the protocol name is always "jdbc". The name of the b subprotocol is determined by the writer of the JDBC driver. C the data source name may also contain information such as user and password; this information can also be provided separately. Several common database connections-- oracle- driver: oracle.jdbc.driver.OracleDriverURL:jdbc:oracle:thin:@machine_name:port:dbname Note: machine_name: the name of the machine where the database is located Port: Port number. Default is 1521 Murray MySQL-driver: com.mysql.jdbc.DriverURL:jdbc:mysql://machine_name:port/dbname Note: machine_name: the name of the machine on which the database resides Port: Port number. Default is 3306-- SQLServer-driver: com.microsoft.jdbc.sqlserver.SQLServerDriverURL:jdbc:microsoft:sqlserver://;DatabaseName= Note: machine_name: name of the machine on which the database resides. Port: Port number. Default is 1433murmurDB2 port-driver: com.ibm.db2.jdbc.app.DB2DriverURL:jdbc:db2:///dbname Note: machine_name: name of the machine on which the database resides Port: Port number. Default is 5000 Murray-

2. Three ways for JDBC to connect to the database

2.1. The first way of implementation

Package com.rk.db.a_jdbc;import java.sql.Driver;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties / * jdbc connects to the database * the first method: establish a database connection by creating the driver class object of the JDBC implementation class * @ author RK * / public class Demo01 {public static void main (String [] args) throws SQLException {/ / jdbc protocol: database subprotocol: host: Port / connected database / / String url = "jdbc:mysql://localhost:3306/testdb" String username = "root"; String password = "root"; / / 1. Create the driver class object Driver driver = new com.mysql.jdbc.Driver (); / / set the user name and password Properties props = new Properties (); props.setProperty ("user", username) Props.setProperty ("password", password) / / 2. Connect to the database and return the connection object Connection conn = driver.connect (url, props); System.out.println (conn);}} 2.2.2.The second implementation

Package com.rk.db.a_jdbc;import java.sql.Driver;import java.sql.DriverManager;import java.sql.Connection;import java.sql.SQLException / * jdbc connection database * second method: use the driver manager class to connect to the database (registered twice, not necessary) * @ author RK * * / public class Demo02 {public static void main (String [] args) throws SQLException {String url = "jdbc:mysql://localhost:3306/testdb"; String username = "root" String password = "root"; Driver driver = new com.mysql.jdbc.Driver (); / / 1. Register drivers (multiple drivers can be registered) DriverManager.registerDriver (driver); / / 2. Connect to a specific database Connection conn = DriverManager.getConnection (url, username, password); System.out.println (conn);}} 2.3. third implementation

Package com.rk.db.a_jdbc;import java.sql.DriverManager;import java.sql.Connection;import java.sql.SQLException / * jdbc connects to the database * third method: use the load driver class to register the driver (recommended to connect to the database) * @ author RK * * / public class Demo03 {public static void main (String [] args) throws ClassNotFoundException, SQLException {String url = "jdbc:mysql://localhost:3306/testdb"; String user = "root" String password = "root"; / / registers the driver Class.forName ("com.mysql.jdbc.Driver") by loading the static code block by getting the bytecode object / / Connect to the specific database Connection conn = DriverManager.getConnection (url,user,password); System.out.println (conn);}} 3, the internal implementation of com.mysql.jdbc.Driver

The main explanation here is that in 2. 3, there is no code to register the instance of com.mysql.jdbc.Driver with DriverManager. The reason lies in the source code of com.mysql.jdbc.Driver.

A static (static) code snippet is provided in the com.mysql.jdbc.Driver class, as follows:

Java.sql.DriverManager.registerDriver (new Driver ())

The complete code is as follows:

-static code snippet is provided in the com.mysql.jdbc.Driver class to actively register java.sql.DriverManager.registerDriver (new Driver ()) with DriverManager; package com.mysql.jdbc;import java.sql.SQLException;public class Driver extends NonRegisteringDriver implements java.sql.Driver {/ Register ourselves with the DriverManager / / static {try {java.sql.DriverManager.registerDriver (new Driver ()) } catch (SQLException E) {throw new RuntimeException ("Can't register driver!");}} / * Construct a new driver and register it with DriverManager * / public Driver () throws SQLException {/ / Required for Class.forName () .newInstance ()}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report