Java JDBC Driver Database Interface settings

Author:Anonymous    Updated:2008-2-26 22:25:56
DriverManager class JDBC is the management of the role between users and drivers. It tracking available drivers, and in the database and the corresponding connection established between the driver. In addition, the DriverManager class also deal with issues such as driver log time constraints and log and track information display affairs.
For simple applications, programmers need in the general directly in the use of such methods is the only DriverManager.getConnection. As the name implies, this method will be established and database connections. JDBC allows users to call DriverManager method getDriver, getDrivers registerDriver and Driver and connect method. However, in most cases, to establish a connection DriverManager class management for the best way to the details.

1, tracking available drivers

DriverManager class includes a Driver category, they have DriverManager.registerDriver by calling their methods were registered. All Driver category must contain a static part. It created such examples, and then loading the instance when DriverManager class to register. In this way, users under normal circumstances would not directly call DriverManager.registerDriver; but in the loading process driven by the driver automatically call. Loading Driver category, and then automatically registered in the DriverManager There are two ways:

By calling the method Class.forName. This will obviously drive-in loading procedures category. Since this has nothing to do with external setting, it recommended the use of this method of loading drivers. Acme.db.Driver loading of the code below:

Class.forName ( "acme.db.Driver");

If acme.db.Driver prepared for loading create examples and examples for the call to the parameters of DriverManager.registerDriver (the present case), then it DriverManager driver list, and can also be used to create connections.

By driver added to the attributes jdbc.drivers in java.lang.System. This is a DriverManager class load driver class name list, separated by a colon: DriverManager class initialization, it jdbc.drivers attribute search system, if the user has entered one or more drivers, DriverManager class, will try to load them. Note how the code below programmers in the ~ /. Hotjava / properties in the importation of three drivers (activated, it will HotJava loaded to the system attribute list):

Jdbc.drivers = foo.bah.Driver: wombat.sql.Driver: bad.test.ourDriver;

DriverManager method of the first of these calls will be automatically loaded driver type.

Note: The second driver loading method to maintain the default environment. If this point can not guarantee that, the method Class.forName explicit call to load each driver has become more security. This is the introduction of specific drivers, because once DriverManager class is initialized, it will no longer check jdbc.drivers attribute list.

In both cases, the new category should be loaded Driver category by calling DriverManager.registerDriver self-registration. As noted above, the category will be automatically loaded implementation of this process.

Because of security reasons, which will be tracked JDBC management category which provides loading drivers. In this way, when open category DriverManager connection, it uses only the local file system or a connection with the same request of the code provided by the loading of drivers.

2, establishing connections

Loading Driver category and DriverManager class registration, they can be used to connect with the database. When you call a method of connecting DriverManager.getConnection request, DriverManager will check each driver to see whether it can establish a connection.

Sometimes there may be a number of JDBC driver can be set to the URL link. For example, and connect to the remote database, you can use JDBC-ODBC Bridge drivers, network protocols common to the JDBC driver or database vendors to provide drivers. In such circumstances, the order of driver testing is essential, because DriverManager it will be used to find the first to successfully connect to the URL of the drivers.

First DriverManager according to the order of registration procedures for the use of each drive (jdbc.drivers listed in the driver always register). Skip code will not trust the drivers, unless they are loading and tried to open the source connected to the same source code.

It passed alternately in each driver on the way Driver.connect call, and they transfer a customer to transfer method DriverManager.getConnection the URL to the drive test procedures, and connect the first to recognize the URL driver .

At first glance this method efficiency is not high, but since it is not possible at the same time load of 10 drivers, each only a few actual process of connecting calls and string comparison.

The following code is usually driven by circumstances (such as JDBC-ODBC Bridge drivers) to connect all the necessary steps examples:

Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver"); / / load the driver

String url = "jdbc: odbc: fred";

DriverManager.getConnection (url, "userID", "passwd");
Previous:Hibernate local use of a simple SQL query
Next:Comprehensive understanding of Java in the String data type
User Reviews
Related Articles
Recommended article
AD