JDBC serves as a canonical interface for database access, where only some interfaces are defined. The specific implementation is done by each database vendor.
An important interface:
1. Publicinterface Driver The interface that each driver class must implement. The Java SQL framework allows multiple database drivers. Each driver should provide a class that implements the Driver interface. DriverManager tries to load as many drivers as it can find, and then, for any given connection request, it lets each driver try to connect to the destination URL in turn. It is strongly recommended that each Driver class be small and separate so that the Driver class can be loaded and queried without introducing a large number of support code. When a Driver class is loaded, it should create its own instance and register the instance with DriverManager. This means that the user can load and register a driver by calling the following program Class.forName("foo.bah.Driver")。例如:MYSQL驱动
Com.mysql.jdbc.Driver
2. Publicinterface Connection connection to a specific database (session). Executes the SQL statement in the connection context and returns the result.
3.publicInterface Statement is used to execute a static SQL statement and return the object to which it produces results.
4. Publicinterface preparedstatement represents the object of the precompiled SQL statement. The SQL statement is precompiled and stored in the PreparedStatement
object. You can then use this object to execute the statement efficiently several times.
Second, the driving mode of loading
1. The most commonly used is the use of Class.forName ("Com.mysql.jdbc.Driver"); This line of code just uses the current class load to load the specific database driver, do not underestimate this simple line of code. Registers the current driver in the DriverManager in the static domain in the driver class.
Static { try { java.sql.DriverManager.registerDriver (new Driver ()); Register drive catch (SQLException E) { throwNew RuntimeException ("Can ' t register driver!" ); } }
2. By looking at the DriverManager source code, we can also use the System.setproperty ("Jdbc.drivers", "....") mode.
String drivers;
try {
Drivers = accesscontroller.doprivileged (new privilegedaction<string> () {
Public String Run () {
Return System.getproperty ("Jdbc.drivers");
}
});
} catch (Exception ex) {
drivers = null;
}
string[] DriversList = Drivers.split (":");
println ("Number of Drivers:" + driverslist.length);
for (String adriver:driverslist) {
try {
println ("DriverManager.Initialize:loading" + adriver);
Class.forName (Adriver, True,
Classloader.getsystemclassloader ());
} catch (Exception ex) {
println ("DriverManager.Initialize:load failed:" + ex);
}
}
3. Most direct (not recommended) way new Com.mysql.jdbc.Driver ();
4. To better use the database driver, JDBC provides us with the DriverManager class. If we do not use the above method, DriverManager initialization will be found through the Serviceloader class in our classpath jar (Database driver package), such as the existence of meta-inf/services/ Java.sql.Driver file, the driver class in the file is loaded.
Accesscontroller.doprivileged (NewPrivilegedaction<void>() { PublicVoid Run () {Serviceloader<Driver> loadeddrivers = Serviceloader.load (Driver.class); Iterator<Driver> Driversiterator =Loadeddrivers.iterator (); /*Load These drivers, so this they can be instantiated. * It May is the case, the driver class may is not there * i.e. there is a packaged driver with the Service class * As implementation of Java.sql.Driver but the actual class * could be missin G. In this case a java.util.ServiceConfigurationError * 'll be thrown at runtime by the VM trying to Loca TE * and load the service. * * Adding a try catch block to catch those runtime errors * If driver not available in CL Asspath but it's * packaged as service and that service are there in classpath. */ Try{ while(Driversiterator.hasnext ()) {driversiterator.next (); } } Catch(Throwable t) {// do nothing } return NULL; } });
How JDBC Loads the database driver