Author: zfive5 (zhaozidong)
Email: zfive5@yahoo.com.cn
During this time, in addition to work, I have been watching Java, plus the original holiday, and I feel that when I write a blog, I will take JDBC to burn, there are many articles about JDBC, so I don't want to write " How to use the big rice white face (must eat, but can not eat only), I have seen an EJB article in 9CBS a few days ago, I firmly and firm my thoughts.
About JDBC usage process is roughly like this:
Class.Forname ("..."). Newinstance (); string url = "...."; String user = "..."; string password = "..."; connection conn = drivermanager.getConnection (URL, User, Password);
// The following operational database does not have anything, Google can come out for N pages.
......
Class.Forname (String ClassName) is done is the DriverManager..RegisterDriver () feature, which can be found in the org.gjt.mm.mysql.driver class (found in the MySQL JDBC driver).
Public class driver extends com.mysql.jdbc.driver {
......
Public driver () throws sqlexception {
Super ();
}
}
Public Class Driver Extends NonRegisteringDriver Implements Java.sql.driver {
...
STATIC {
Try {
// This is a key call, generate an instance, and then call registerDriver ()
Java.sql.driverManager.RegisterDriver (New Driver ());
} catch (sqlexception e) {
Throw new runtimeException ("Can't Register Driver!");
}
}
......
Public driver () throws sqlexception {
// Required for class.Forname (). Newinstance ()
}
}
So what is DRIVERMANAGER.REGISTERDRIVER () to complete?
Public Static Synchronized Void RegisterDriver (java.sql.driver driver)
Throws sqlexception {
......
// Save the Driver (previously generated drive instance) into a global vector (Singthon) to return this JDBC driver instance through the agreed database connection string.
Driverinfo di = new driverinfo ();
Di.driver = driver;
Di.driverclass = driver.getClass ();
Di.driverclassname = di.driverclass.getname ();
Drivers.addelement (di);
......
}
Take a look at the implementation of the drivermanager.getconnection () function, getConnection (String String, String Sring, String String) function is actually a package of getConnection (String, Properties Info, ClassLoader CallerCl), and you can see this function directly here. . Connection.getConnection (String Url, Properties Info, CallerCl) {
......
// Function core partial code, is a comparison connection string, returns a Driver instance, then call this Driver's connection to return a data connection object, in which you can perform the database through this object, use Statement, PerparedStatement, CallaBleState, and ResultSet interface can be Database connection objects are implemented, in all JDBC drivers, these interfaces also require specific implementation.
SQLEXCEPTION REASON = NULL;
For (int i = 0; i Driverinfo di = (driverInfo) Drivers.ementat (i); IF (getCallerClass (CallerCl, Di.DriverclassName)! = di.driverclass) { CONTINUE; } Try { Connection result = di.driver.connect (url, info); IF (result! = null) { // Success! Return (Result); } } catch (sqlexception ex) { IF (REASON == NULL) { REASON = EX; } } } // Didn't find a matching driver, return a SQLException exception ...... } See if you know the implementation mechanism of JDBC driver, do you want to write a driver? OK, we can write XML JDBC drivers, just implement the part of Java.sql.driver and java.sql.connection interface, you can write this simple version of JDBC driver (if you write a complete driver, I will not To write, there are too many methods to achieve, don't despise it here, don't believe you can try it). In this case, I think of a case where the PHP operation database method is not unified. At this time, I feel that PHP can refer to the style specification of JDBC, design a so-called "PDBC"! I saw the mode - factory method mode in JDBC. Drivermanager RegisterDriver () Driver ------------------------ CONNECTION Connect () | | | | XXX_Driver () xxx_connection Connect () Drivermanager. GetConnection () JavaSDK frame designers are really not easy! The mode ideas are used to use this. Different, unified use!