Java linkage database inside

zhaozj2021-02-16  79

Java.sql.driver, jdbc.sql.driver, jdbc.sql.connection in java.sql package, provides the program developer's unified development interface, database provider provides corresponding implementation, and as long as the program developers know what methods do they know these interfaces Yes. But we can go deep into some things to do this, and you can learn the programming mode (such as Interface mode, etc.). 1, the source code of class.Forname (String classname) is:

public finalclass Class implements java.io.Serializable {... public static Class forName (String className) throws ClassNotFoundException {return forName0 (className, true, ClassLoader.getCallerClassLoader ());} ...}

About ForName0 Please check your own JDK Source. The specified Class is loaded into the JVM. (Note Class Loading, Initialization Process) The STATIC block (below) 2 Sun's JDBCODBCDRIVER source code is performed during the loading process:

public class JdbcOdbcDriver extends JdbcOdbcObjectimplements JdbcOdbcDriverInterface {... / ** * connect to DB * / public synchronized Connection connect (String s, Properties properties) throws SQLException {if (JdbcOdbcObject.isTracing ()) JdbcOdbcObject.trace ( "* Driver.connect (" S ") "); if (! acceptsurl (s)) Return NULL; if (HDBC! = 0) {Disconnect (HDBC); CloseConnection (HDBC); HDBC = 0;} if (! initialize () ) {return null;} else {jdbcOdbcConnection jdbcodbcconnection = new jdbcOdbcConnection (OdbcApi, hEnv, this); jdbcodbcconnection.initialize (getSubName (s), properties, DriverManager.getLoginTimeout ()); jdbcodbcconnection.setURL (s); return jdbcodbcconnection;} } static {if (JdbcOdbcObject.isTracing ()) JdbcOdbcObject.trace ( "JdbcOdbcDriver class loaded"); JdbcOdbcDriver jdbcodbcdriver = new JdbcOdbcDriver (); try {DriverManager.registerDriver (jdbcodbcdriver);} catch (SQLException sql Exception) {if (JDBCODBCOBJECT.ITRACING ()) JDBCODBCOBJECT.TRACE ("Unable to register driver");}}} public interface jdbcodbcdriverinterface extends driver {...} 3 connection process

jdbc.sql.Connection con = DriverManager.getConnection ( "jdbc: odbc: pubs", "sa", ""); public class DriverManager {public static synchronized Connection getConnection (String url, String user, String password) throws SQLException {java .util.Properties info = new java.util.Properties (); // Gets the classloader of the code that called this method, may // be null ClassLoader callerCL = DriverManager.getCallerClassLoader ();. if (! user = null) {INFO.PUT ("User", User;} if (password! = Null) {info.put ("password", password);} returnne (URL, INFO, CALLERCL);} private static synchronized connection GetConnection (String Url, Java.util.properties Info, ClassLoader CallerCl) throws Sqlexception {... connection result = di.driver.connect (url, info); ...}} 4

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

New Post(0)