JDBC Overview (III)

Source: Internet
Author: User

JDBC Overview (III)

3-drivermanager

3.1 Overview

The drivermanager class is the management layer of JDBC and acts on users and drivers.Program. It tracks available drivers and establishes connections between the database and the corresponding drivers. In addition, the drivermanager class also processes transactions such as the driver logon time limit and the display of logon and tracing messages.

For simple applications, the only method that programmers need to directly use in this class is drivermanager. getconnection. As shown in the name, this method establishes a connection with the database. JDBC allows you to call the drivermanager method getdriver, getdrivers, registerdriver, and driver method connect. However, in most cases, it is best to set up the connection details for drivermanager class management.

3.1.1 trace available drivers

The drivermanager class contains a column of driver classes that have been registered by calling the drivermanager. registerdriver method. All driver classes must contain a static part. It creates an instance of this class and registers the drivermanager class when the instance is loaded. In this way, the user will not directly call drivermanager. registerdriver, but will be automatically called by the driver when loading the driver. There are two ways to load the driver class and then automatically register in drivermanager:

Call class. forname. This will explicitly load the driver class. Because this is irrelevant to external settings, we recommend that you use this method to load the driver. BelowCodeLoad class acme. DB. DRIVER:
Class. forname ("Acme. DB. Driver ");

If you set Acme. DB. the driver program creates an instance when it is loaded and calls the drivermanager with the instance as the parameter. registerdriver (this should have been the case) is included in the driver list of drivermanager and can be used to create a connection.

Add the driver to Java. lang. JDBC. in drivers, this is a list of Driver Class names loaded by the drivermanager class, separated by colons: it searches for System Properties JDBC when initializing the drivermanager class. drivers. If you have entered one or more drivers, the drivermanager class will try to load them.

The following code describes how programmers can ~ /. Enter three Driver classes in hotjava/properties (at startup, hotjava loads them to the system property list ):

JDBC. Drivers = Foo. Bah. DRIVER: Wombat. SQL. DRIVER: Bad. Test. ourdriver;

The first call to the drivermanager method will automatically load these driver classes. Note: The second method for loading the driver requires a persistent preset environment. If this is not guaranteed, it is safer to call the class. forname method to explicitly load each driver. This is also the method for introducing specific drivers, because once the drivermanager class is initialized, it will no longer check the JDBC. Drivers attribute list.

In the preceding two cases, the newly loaded driver class must be self-registered by calling the drivermanager. registerdriver class. As described above, this process is automatically executed when a class is loaded.

For security reasons, the JDBC management layer will track which class loader provides which driver. In this way, when the drivermanager class opens the connection, it only uses the driver provided by the local file system or the class loader that identical to the code that sends the connection request.

3.1.2 establish a connection

After loading the driver class and registering it in the drivermanager class, they can be used to establish a connection with the database. When the drivermanager. getconnection method is called to send a connection request, drivermanager checks Each driver to see if it can establish a connection. Sometimes there may be multiple JDBC drivers that can be connected to a given URL. For example, when connecting to a given remote database, you can use the JDBC-ODBC bridge driver, JDBC to the generic network protocol driver, or the driver provided by the database vendor. In this case, the order to test the driver is crucial because drivermanager will use the first driver it finds that can successfully connect to the given URL.

First, drivermanager tries to use each driver in the registration order (drivers listed in JDBC. Drivers are always registered first ). It skips untrusted drivers of the Code unless the source that loads them is the same as the source of the Code that tries to open the connection.

It calls the method driver on each driver in turn. connect, and pass them to the user to start passing to the method drivermanager. getconnection URL to test the driver and connect the first driver that recognizes the URL.

This method seems inefficient at the beginning, but since it is impossible to load dozens of drivers at the same time, each connection actually requires only a few process calls and string comparison.

The following code is an example of all the steps required to establish a connection with a driver, such as a JDBC-ODBC bridge DRIVER:

Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver"); // load the driver
String url = "JDBC: ODBC: Fred ";
Drivermanager. getconnection (URL, "userid", "passwd ");

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.