Three ways to register the JDBC driver

Source: Internet
Author: User

About Driver Packages

to register the driver in JDBC, first import the corresponding package, such as Mysql-connector-java-5.0.8-bin.jar. A driver package is a bridge between Java and a specific database, developed by a database vendor. Each database corresponds to a drive jar, and even each version of the database has its own version of the driver jar.

About the Java.sqldrivermanger class

Java 's driver management class. Manages a set of JDBC drivers.

The Javax.sql.DataSource interface is something new in the JDBC 2.0 API that provides another way to connect to a data source. using The DataSource object is the preferred method for connecting to the data source.

See the Java documentation for a deeper understanding .

Divermanager.classHas a property in it.Drivers, it is actually aVector (vectors). A number of drivers can be added to the list whenDriverManagerTo pick up the connection, ifDriversThere are many drivers, it willDriversAll the drivers inside.Urland create the connection simultaneous came inUrlCompare each other, encounter the correspondingUrl, the connection is established.

three ways to register a drive:

ADrivermanager.registerdriver (New Com.mysql.jdbc.Driver ());
Will causedrivermanager specifically:  
1 When the load is registered for a driver (see the third sign-up method), the time of instantiation is registered again. So two times.  
2 ( import ) ,< Span style= "color: #003399;" > Thus the specific driver produces a dependency. Extension code is not convenient.  

(ii)

Setting up the registration driver through the system's properties

If you want to register multiple drivers, then system.setproperty ("Jdbc.drivers", "Com.mysql.jdbc.Driver:com.oracle.jdbc.Driver");
Although there is no dependency on the specific driver classes, registration is not easy, so it is seldom used.


(iii)class.forname ("Com.mysql.jdbc.Driver"); (for further understanding of this code, refer to another article, "from Class.forName () to the loading mechanism of a Class")

It is recommended that this approach not be dependent on the specific driver class (that is, the import package is not necessary ).
In fact, this is just the com.mysql.jdbc.Driver.class this class loaded into, but the key is that, in
In this class, there is a static block, as follows:
static{
try{
Java.sql.DriverManager.registerDriver (New Driver ());
}catch (SQLException e) {
throw new RuntimeException ("Can ' t register driver!");
}
}
Just because of this block of code, let the class in the load when the driver registered in the!

Three ways to register the JDBC driver (RPM)

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.