Jdbctm guide: Getting started 3-drivermanager

Source: Internet
Author: User

Content:
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 such as driver logon time restrictions and logon and
Tracks transactions such as message display.

For simple applications, the only method that programmers need to use directly in this class
Is drivermanager. getconnection. As shown in the name, this method will be created
Database connection. JDBC allows users to call the drivermanager Method
Getdriver, getdrivers, registerdriver, and driver Methods
Connect. However, in most cases, let drivermanager Class Management establish the connection details
This is the best practice.

3.1.1 trace available drivers
The drivermanager class contains a column of driver classes that have passed the call Method
Drivermanager. registerdriver registers itself. All drivers
The class must contain a static part. It creates an instance of this class and then loads
The drivermanager class is used for instance registration. In this way, the user will not normally
Call drivermanager. registerdriver directly.
Automatically called by the driver. Load the driver class, and then automatically
There are two ways to register in:

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

If you write acme. DB. Driver as the created instance when loading and call
The drivermanager. registerdriver parameter (this should have been the case ),
The driver list of drivermanager and can be used to create a connection.

Add the driver to the JDBC. Drivers attribute of Java. Lang. system.
This is a list of Driver Class names loaded by the drivermanager class
Separation: When initializing the drivermanager class, it searches for the system attribute JDBC. Drivers,
If you have already 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 will load it 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 you cannot
Ensure that the calling method class. forname explicitly loads each driver.
Security. This is also the method for introducing a specific driver, because once the drivermanager class is initialized, it
The JDBC. Drivers attribute list will no longer be checked.

In both cases, the newly loaded Driver Class must call drivermanager. registerdriver
Class for self-registration. 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
Drivers. 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 is the same as the code that sends the connection request.

3.1.2 establish a connection
Load the driver class and register it in the drivermanager class.
Establish a connection to the database. When the drivermanager. getconnection method is called to send a connection
When a request is sent, 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
Given a remote database connection, you can use the JDBC-ODBC bridge driver, JDBC
Common network protocol drivers or drivers provided by database vendors. In this case
The order of the test driver is crucial because drivermanager will use it to find
The first driver that can be successfully connected 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 will skip the code and cannot
Trusted drivers, unless they are loaded with the same source as the Code trying to open the connection.

It calls the method driver. Connect on each driver in turn and transmits
The user passes the URL to the drivermanager. getconnection method
Then 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,
Therefore, each connection requires only a few process calls and string comparison.

The following code is typically used with drivers (such as JDBC-ODBC bridge drivers)
Examples of all steps required to establish a connection:

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.