Several methods and simple parsing of JDBC Connection database

Source: Internet
Author: User
Tags db2

The first thing to know about JDBC: the technique of sending SQL statements using Java code is JDBC technology. That is, JDBC is an interface for different databases (Oracle, MySQL, SQL Server). ) of the 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 + 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 ——————
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 resides;
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 resides (this machine generally defaults 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 resides;
Port: Port number, default is 1433

———————— –DB2 ———————— –
Drive: Com.ibm.db2.jdbc.app.DB2Driver
url:jdbc:db2://<:p Ort>/dbname
Note: machine_name: The name of the machine where the database resides; (port default 5000)
take MySQL for example here.
Jar Package Required: Mysql-connector-java-5.1.7-bin.jar
The code is as follows:

Importjava.sql.Connection;ImportJava.sql.Driver;ImportJava.sql.DriverManager;Importjava.util.Properties;Importorg.junit.Test; Public classdemo1{//the URL to connect to the database first        PrivateString url = "Jbdc:mysql://localhost:3306/demo";/*JDBC Protocol: Database sub-protocol: Host: Port/Connected Database*/        PrivateString user = "root";//Database user name        PrivateString password = "root";//Database Password        /*The first of these methods*/@Test Public voidTest1 ()throwsexception{//1. Creating a Driver class objectDriver Driver =NewCom.mysql.jdbc.Driver ();//need to import the jar package mentioned above//set user name and passwordProperties Pro =NewProperties (); Pro.setproperty ("User", user); Pro.setproperty ("Password", password); //2. Connect to the databaseConnection conn =Driver.connect (Url,pro); //Test If the connection is successfulSYSTEM.OUT.PRINTLN (conn); }    /*second method (use the Drive Manager class to connect to the database)*/@Test Public voidTest2 ()throwsexception{//To create a driver class objectDirver Dirver =NewCom.mysql.jdbc.Dirver (); /*Mysql*/        //Driver driver2 = new Com.oracle.jdbc.Driver ();/*oracle*///1. Register the driver (you can register multiple)Dirvermanager.registerdirver (dirver); //2. Establish a connection to the databaseConnection conn =drivermanager.getconnection (URL, user, password); //Test If the connection is successfulSYSTEM.OUT.PRINTLN (conn); }   }

After running, a successful result should show 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. This is because there is a static block of code in the Driver.class file:

//    // Register ourselves with the DriverManager     //    Static {        try  {            java.sql.DriverManager.registerDriver (new  Driver ());         Catch (SQLException E) {            thrownew runtimeexception ("Can ' t register driver!" );        }    }
View Code

The Registerdriver (new Driver ()) is executed when the driver class object Driver is created, so Dirvermanager.registerdirver (Dirver) is omitted from the second method and the statement is correct.

Based on the above analysis, it is possible to register a driver by loading a static block of code by getting a bytecode object ("Com.mysql.jdbc.Driver") for the creation of the driver class object statement ("Class.forName"). The double quotation mark in parentheses is the name of the package in which the Dirver.class file is located (inside the jar package mentioned above). The complete code is as follows:

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

Thus, this part of the connection has been successful. Simple analysis of the code helps to understand memory rather than memorization to reach ingenious. This example is done in MySQL database, similar to other databases.

Several methods and simple parsing of JDBC Connection database

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.