JDBC Series: (1) Connect to database through JDBC

Source: Internet
Author: User
Tags db2

    • 1. What is JDBC

    • 2. Three ways of JDBC Connection database

    • 2.1. The first way of realization

    • 2.2. The second way of realization

    • 2.3. The third way of realization

    • 3, the internal realization of Com.mysql.jdbc.Driver

1. What is JDBC


Techniques for sending SQL statements using Java Code (Programs)

Using JDBC to send SQL prerequisites needs to know: The database's IP address, port, data name, user name, and password.


JDBC Url= protocol name + sub-protocol name + data source name. The a  protocol name is always "JDBC". The b  sub-protocol name is determined by the creator of the JDBC driver. The c  data source name may also contain information such as the user and password, which can also be provided separately. Several common database connections-------------------------------Oracle------------------driver: Oracle.jdbc.driver.oracledriverurl:jdbc:o Racle:thin: @machine_name:p ort:dbname Note: machine_name: The name of the machine where the database resides;       port: port number, The default is 1521-------------------------------MySQL-------------------driver: com.mysql.jdbc.driverurl:jdbc:mysql://machine _name:port/dbname Note: machine_name: The name of the machine where the database resides;       port: port number, default 3306     ---------------------------sql  Server------------------Driver:com.microsoft.jdbc.sqlserver.sqlserverdriverurl:jdbc:microsoft:sqlserver://< machine_name><:p ort>;D atabasename=<dbname> Note: machine_name: The name of the machine where the database resides;       port: Port number, The default is 1433--------------------------DB2--------------------------driver: com.ibm.db2.jdbc.app.db2driverurl:jdbc:db2:// <machine_name><:p ort>/dbname Note: machine_name: The name of the machine where the database resides;Nbsp;     port: Port number, default is------------------------------------------------------- 


2. Three ways of JDBC Connection database




2.1. The first way of realization


Package Com.rk.db.a_jdbc;import Java.sql.driver;import Java.sql.connection;import java.sql.sqlexception;import java.util.properties;/** * JDBC Connection Database * The first method: establish a database connection by creating a driver class object for the JDBC implementation class * @author RK * */public class Demo01{public static void Main (string[] args) throws sqlexception{//JDBC Protocol: Database sub-protocol: Host: Port/connected database//string URL = "jdbc:mysql://localhost:3306/ TestDB "; String username = "root"; String password = "root";//1. Create driver class object Driver Driver = new Com.mysql.jdbc.Driver ();//set user name and Password properties props = new Proper Ties ();p rops.setproperty ("User", username);p rops.setproperty ("password", password);//2. Connect to the database and return to the Connection object connection conn = driver.connect (URL, props); SYSTEM.OUT.PRINTLN (conn);}}

2.2. The second way of realization


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 * The second method: connect to the database using the Drive Manager class (registered two times, 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";D river Driver = new Com.mysql.jdbc.Driver ();//1. Registering 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. The third way of realization


Package Com.rk.db.a_jdbc;import Java.sql.drivermanager;import Java.sql.connection;import java.sql.SQLException;/** * JDBC Connection Database * Third method: Use the Load driver class to register the driver (this is the recommended way 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";//Load a static block of code by getting a bytecode object to register the driver Class.forName ("Com.mysql.jdbc.Driver");// Connect to a specific database connection conn = Drivermanager.getconnection (Url,user,password); SYSTEM.OUT.PRINTLN (conn);}}

3, the internal realization of Com.mysql.jdbc.Driver



Here is the main explanation of a problem, that is, in 2.3, and did not write code with DriverManager to Com.mysql.jdbc.Driver instance registration, the reason is Com.mysql.jdbc.Driver source.

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:


The static code snippet is provided in the------Com.mysql.jdbc.Driver class and is actively registered with DriverManager Java.sql.DriverManager.registerDriver (new  Driver ());p ackage 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 ()     }}




JDBC Series: (1) Connect to database through JDBC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.