JDBC series tutorial (ii) --- driver settings
Drivermanager
This overview is taken from jdbctm database access from javatm: a tutorial and annotated reference. It is being compiled by mongooft. This book is both a JDBC tutorial and an authoritative reference manual, which will be published by Addison-Wesley in the spring of 1997 as part of the Java series.
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 the JDBC. Drivers attribute of Java. Lang. system. This is a list of Driver Class names loaded by the drivermanager class, separated by a colon: 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 to 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");