JDBC Overview (3)

zhaozj2021-02-16  55

3 - DriverManager3.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. 3.1.1 Tracking Available Drivers The 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 write acme.db.driver as loaded as loaded, Create an instance, and call DriverManager.RegisterDriver as parameters (this This is, then it is in the DriverManager's driver list and can be used to create a connection. This is a list of driver class names loaded by the DriverManager class by adding the driver to java.drivers, which is a list of driver matrios loaded by the DRIVERMANAGER class. Separate by the colon: Initialize the DriverManager class, it searches for system properties JDBC.DRIVERS, if The user has entered one or more drivers, and the DriverManager class will attempt to load them. 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.ourcesDriver; 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.

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

New Post(0)