JDBCTM Guide: Getting Started 3-drivermanager

Source: Internet
Author: User
Tags file system connect odbc string
Content:
3-drivermanager
3.1 Overview
The DriverManager class is a JDBC management layer that acts between users and drivers.
It tracks the available drivers and establishes a connection between the database and the corresponding drivers.
In addition, the DriverManager class also handles such things as driver logon time limits and logon and
A transaction that tracks the display of messages.

For simple applications, the only way the average programmer needs to use it directly in this class
It's drivermanager.getconnection. As the name shows, the method creates a
The connection to the database. JDBC allows users to invoke DriverManager methods
Methods of Getdriver, Getdrivers and Registerdriver and Driver
Connect But in most cases, let DriverManager class management establish the connection of the fine
Festival is the best policy.


3.1.1 Tracking Available Drivers
The DriverManager class contains a list of Driver classes that have been passed by calling methods
Drivermanager.registerdriver has registered himself. All Driver
Class must contain a static part. It creates an instance of the class, and then loads the
instance, the DriverManager class is registered. In this way, the user will normally not
Call Drivermanager.registerdriver directly, but when the driver is loaded
Called automatically by the driver. Load the Driver class, and then automatically DriverManager
There are two ways to register in:


Class.forName by calling the method. This will explicitly load the driver class. As this
Independent of external settings, this method of loading drivers is recommended. The following code
Load Class Acme.db.Driver:
Class.forName ("Acme.db.Driver");

If Acme.db.Driver is written to create an instance at load time, and the instance is called
The drivermanager.registerdriver of the argument (as it should be), it is
DriverManager list of drivers and can be used to create a connection.


By adding the driver to the Java.lang.System property jdbc.drivers
This is a list of driver class names loaded by the DriverManager class, by a colon
Partitioning: When initializing the DriverManager class, it searches for system Properties jdbc.drivers,
If the user has entered one or more drivers, the DriverManager class will attempt to load them.
The following code shows how programmers can enter three driver classes in ~/.hotjava/properties (
When started, HotJava will load it into the System Properties 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 of loading a driver requires a persistent preset environment. If this cannot be
Guaranteed, the call method class.forname the explicit loading of each driver appears to be more
Safety. This is also a way to introduce a particular driver, because once the DriverManager class is initialized, it
The list of Jdbc.drivers properties will no longer be checked.

In both cases, the newly loaded Driver class is called by calling the Drivermanager.registerdriver
class to register themselves. As mentioned above, this process is performed automatically when the class is loaded.

For security reasons, the JDBC management layer will track which class loader provides
Driver. This way, when the DriverManager class opens the connection, it uses only the
The driver provided by the local file system or the same class loader as the code that issued the connection request.


3.1.2 Establish a connection
After loading the Driver classes and registering them in the DriverManager class, they can be used to
According to the library to establish the connection. When the Drivermanager.getconnection method is invoked to emit a connection
When requested, 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, with
For a given remote database connection, you can use the Jdbc-odbc Bridge driver, JDBC to
A generic network protocol driver or a database vendor-supplied driver. In this case
Testing the order of the drivers is critical, because DriverManager will use what it finds
The first driver that can successfully connect to a given URL.

First DriverManager try to use each driver in the order of registration
(The drivers listed in Jdbc.drivers are always registered first). It will skip code that cannot be
Trusted drivers unless they are loaded with the same source as the code that attempted to open the connection.

It invokes method Driver.connect on each driver by turns and passes them
The URL that the user begins to pass to the method drivermanager.getconnection to the drive
To test the program and then connect the first driver to recognize the URL.

This approach looks inefficient at first, but because it is not possible to load dozens of drivers at the same time,
So each connection actually takes only a few procedure calls and string comparisons.

The following code is typically used with a driver (for example, the Jdbc-odbc bridge driver)
Examples of all the steps required to establish a connection:

Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver"); Load 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.