JDBC Basic Tutorial Drive Settings

xiaoxiao2021-03-06  71

[text]

1. Overview The DRIVERMANAGER class is the management layer of JDBC, which acts between users and drivers. It tracks the available drivers and establishes a connection between the database and the corresponding driver. In addition, the DRIVERMANAGER class also handles transactions such as the display of the driver login time limit and login and tracking messages.

For simple applications, the unique way to use directly in this class is DriverManager.getConnection. As indicated by the name, the method will establish a connection to the database. JDBC allows users to call DriverManager's methods with GetDriver, GetDrivers, and RegisterDriver and Driver. Connect. But in most cases, let DRIVERMANAGER class management establish the details of the connection. 1. Tracking the Available Driver DriverManager class contains a column DRIVER class that has been registered by calling method DriverManager.RegisterDriver. All DRIVER classes must contain a static part. It creates an instance of this class and then register when the DriverManager class is loaded. In this way, DriverManager.RegisterDriver will not directly call DRIVERMANAGER.REGISTERDRIVER directly; it is automatically called by the driver when the driver is loaded. Load the Driver class and then automatically register in the DriverManager: By calling methods Class.Forname. This will explicitly load the driver class. Since this is not related to external settings, it is recommended to use this method of loading the driver. The following code load class acme.db.driver:

Class.Forname ("acme.db.driver"); if you create an instance when you write acme.db.driver as loading, and call DriverManager.RegisterDriver as the parameter (this), it drives it on DriverManager In the list of programs and can be used to create a connection. Add the driver to the Java.lang.System's properties JDBC.DRIVERS. This is a list of driver class names loaded by the DriverManager class, separated by colon: When the DriverManager class is initialized, it searches for system properties JDBC.DRIVERS, if the user has entered one or more drivers, the DriverManager class will attempt to load they. The following code shows how the programmer enters three driver classes in ~ / .hotjava / Properties (when startup, Hotjava will load it into the system properties list):

JDBC.DRIVERS = foo.bah.driver: wombat.sql.driver: Bad.test.ordriver; the first call to the DriverManager method will automatically load these driver classes. Note: The second method of loading the driver requires a long-lasting preset environment. If you can't guarantee this, the call method class.Forname Explicitly loading each driver is more secure. This is also a method of introducing a particular driver because once the DRIVERMANAGER class is initialized, it will no longer check the JDBC.DRIVERS attribute list. In both cases, the newly loaded Driver class is self-registration by calling the DriverManager.RegisterDriver class. This process will be automatically executed as described above. Due to security, the JDBC management will track which type of loader provides which driver. Thus, when the DRIVERMANAGER class opens, it only uses the local file system or the driver provided by the same class loader as the code issued by the connection request. 2. Establish a connection to load the Driver class and register in the DRIVERMANAGER class, they can be used to connect to the database. DRIVERMANAGER checks each driver when the DRIVERMANAGER.GETCONNECTION method issues a connection request. Sometimes there may be multiple JDBC drivers to connect to a given URL. For example, when connecting to a given remote database, you can use the JDBC-ODBC bridge driver, JDBC to the universal network protocol driver or the driver provided by the database vendor. In this case, the order of the test driver is critical because DriverManager will use the first driver that can be successfully connected to a given URL. First DriverManager is trying to use each driver in the order of registration (the driver listed in JDBC.DRIVERS is always registered first). It will skip the code untrusted drivers unless the source loaded is the same as the source of the code attempting to open the connection. It calls method driver.connect on each driver and passes the user to the URL of the method DriverManager.getConnection, and then connects the first driver that recognizes the URL. This method has no efficiency, but since dozens of drivers are not loaded simultaneously, each connection is actually only a few process calls and string comparisons. The following code is an example in which all steps needed to connect to a connection with a driver (such as JDBC-ODBC Bridge Driver): Class.Forname ("Sun.jdbc.odbc.jdbCodbcDriver"); // Loading Driver String Url = "JDBC: ODBC: FRED"; DriverManager.getConnection (URL, "UserID", "Passwd");

转载请注明原文地址:https://www.9cbs.com/read-92540.html

New Post(0)