Several methods and simple analytic __JDBC of JDBC Connection database

Source: Internet
Author: User
Tags db2 driver manager

The first thing to know about JDBC: the technology for sending SQL statements using Java code is JDBC technology. That is, JDBC is an interface for different databases (Oracle, MySQL, SQL Server ...). ) operation. Prerequisites for sending SQL statements using JDBC:
Log on to the database server (connect to the database server)
The IP address of the database
Port
Database user Name
Password


JDBC Url= protocol name + Child protocol name + data source name.
A protocol name is always "JDBC".
The B child protocol name is determined by the creator of the JDBC driver.
The C data Source name may also contain information such as user and password, which can also be provided separately.
Several common database connections

——————————-Oracle ——————
Drive: Oracle.jdbc.driver.OracleDriver
URL:jdbc:oracle:thin: @machine_name:p ort:dbname
Note: machine_name: The name of the machine where the database is located;
Port: Port number, default is 1521

——————————-MySQL ——————-
Drive: Com.mysql.jdbc.Driver
Url:jdbc:mysql://machine_name:port/dbname
Note: machine_name: The name of the machine where the database is located (this machine is generally default to localhost);
Port: Port number, default 3306

————————— SQL Server ——————
Drive: Com.microsoft.jdbc.sqlserver.SQLServerDriver
url:jdbc:microsoft:sqlserver://<:p ort>;D atabasename=
Note: machine_name: The name of the machine where the database is located;
Port: Port number, default is 1433

———————— –db2 ———————— –
Driver: Com.ibm.db2.jdbc.app.DB2Driver
url:jdbc:db2://<:p ort>/dbname
Note: machine_name: The name of the machine on which the database resides (port default 5000)
here, for example, MySQL. The jar package required for the
: Mysql-connector-java-5.1.7-bin.jar
Code is as follows:

Import java.sql.Connection;
Import Java.sql.Driver;
Import Java.sql.DriverManager;

Import java.util.Properties;
Import Org.junit.Test; public class demo1{//URL of the first connection to the database private String URL = "Jbdc:mysql://localhost:3306/demo";/*JDBC protocol: Database Child Protocol
            : Host: Port/Connected database/private String user = "root";//database user name private string password = "root";//Database Password//* The first method is * * @Test public void test1 () throws exception{//1. Create driver class object Driver Driv
        ER = new Com.mysql.jdbc.Driver ()//need to import the jar package mentioned above//set username and Password Properties Pro = new properties ();
        Pro.setproperty ("user", user);
        Pro.setproperty ("password", password);
        2. Connection database Connection conn = Driver.connect (Url,pro);
    Test whether the connection succeeded SYSTEM.OUT.PRINTLN (conn); }/* The second method (using the Driver manager class to connect to the database)//@Test public void Test2 () throws exception{//Create driver class Object Dirver dirver = new Com.mysql.jdbc.Dirver ();   

/*mysql*///driver driver2 = new Com.oracle.jdbc.Driver ()/*oracle*///1. Register the driver (you can register multiple) Dirve
        Rmanager.registerdirver (Dirver);
        2. Establish a connection to the database Connection conn = drivermanager.getconnection (URL, user, password);
    Test whether the connection succeeded SYSTEM.OUT.PRINTLN (conn); }   

}

After running, the success result should display similar information:

Analysis : In the Test2 method, create the driver class object New Com.mysql.jdbc.Dirver (); After registering the driver dirvermanager.registerdirver (dirver); has actually been registered two times. Because there is a block of static code in the Driver.class file:

---------------------------------------------

    ///
    Register ourselves with the DriverManager
    //
    static {
        try {
            java.sql.DriverManager.registerDriver (new Driver ());
        } catch (SQLException E) {
            throw New RuntimeException ("Can ' t Register driver!");
        }
    

That is, the Registerdriver (New Driver ()) is executed when the driver class object is created Driver, so the second method omits Dirvermanager.registerdirver (Dirver), and the statement is correct.

Based on the above analysis, we can load the static code block by the way of the byte code object to register the driver , that is to replace the creation of the driver class object statement with class.forname ("Com.mysql.jdbc.Driver"); The double quotation mark in parentheses is the package name where the Dirver.class file resides (the jar package mentioned above). The complete code is as follows:

@Test public
    void Test3 () throws exception{
        //To load a static code block in the form of a bytecode object, registering the driver
        class.forname (" Com.mysql.jdbc.Driver ");
        2. Connect to the specific database
        Connection conn = drivermanager.getconnection (URL, user, password);
        SYSTEM.OUT.PRINTLN (conn);
    }

Accordingly, this part of the connection has been successful. A simple analysis of the code helps to understand memory rather than rote memorization to achieve ingenious. This example is done in a MySQL database, similar to other databases.

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.