Java Database Interface JDBC Basics Tutorial driver settings

Source: Internet
Author: User
Tags file system interface connect odbc
Basic Tutorials | data | Database DriverManager class is the management layer of JDBC 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 handles transactions such as driver logon time limits and the display of logon and trace messages.

For simple applications, the only way that the average programmer needs to use it directly in this class is drivermanager.getconnection. As the name shows, this method establishes a connection to the database. JDBC allows users to invoke Dr
Use UC monthly free SMS Sina dot pass the weather
Why do Americans hit the wrong target? 1 beat net Caiso wrist watch 58 percent


Ivermanager methods of Getdriver, Getdrivers and Registerdriver and Driver methods Connect. In most cases, however, it is best to have the details of DriverManager class management establish connections.

1, Tracking available drivers

The DriverManager class contains a list of Driver classes that have been registered to themselves by calling method Drivermanager.registerdriver. All Driver classes must contain a static part. It creates an instance of the class, and then registers the DriverManager class when the instance is loaded. This allows the user to normally not call Drivermanager.registerdriver directly, but is automatically invoked by the driver when the driver is loaded. There are two ways to load the Driver class and then automatically register in DriverManager:

Class.forName by calling the method. This will explicitly load the driver class. Because this is not related to external settings, it is recommended that you use this method of loading the driver. The following code loads the class Acme.db.Driver:

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

If Acme.db.Driver is written to create an instance at load time and the drivermanager.registerdriver that is the parameter of the instance is invoked (as it should be), it is in the list of DriverManager drivers and can be used to create the 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, separated by colons: When the DriverManager class is initialized, it searches for system Properties jdbc.drivers, and if the user has entered one or more drivers, DriverManager Class will attempt to load them. The following code shows how a programmer 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 is not guaranteed, it is more secure to invoke the method class.forname to explicitly load each driver. This is also a way to introduce a particular driver, because once the DriverManager class is initialized, it will no longer check the list of jdbc.drivers properties.

In both cases, the newly loaded Driver class will register itself by calling the Drivermanager.registerdriver class. 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 which driver. This way, when the DriverManager class opens the connection, it uses only the local file system or the driver provided by the same classloader as the code that issued the connection request.

2, establish the connection

After loading the Driver classes and registering them in the DriverManager class, they can be used to establish a connection to the database. When the Drivermanager.getconnection method is invoked to issue 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 a common network protocol driver, or a database vendor-supplied driver. In this case, the order of the test driver is critical, because DriverManager will use the first driver it finds that can successfully connect to the given URL.

First DriverManager attempts to use each driver in the order of registration (the drivers listed in Jdbc.drivers are always registered first). It skips drivers that are not trusted by the code unless the source that loads them is the same as the source of the code attempting to open the connection.

It invokes method Driver.connect on each driver and passes them the URL that the user starts passing to the method drivermanager.getconnection to test the driver, and then connects 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, it actually takes a few procedure calls and string comparisons for each connection.

The following code is an example of all the steps required to establish a connection with a driver, such as the Jdbc-odbc Bridge driver, in general:

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.