How does JDBC connect to a database?

zhaozj2021-02-17  72

JDBC.SQL.Driver D = (DRIVER) Class.Forname ("Sun.jdbc.odbc.jdbcodbcDriver"); jdbc.sql.connection con = DRIVERMANAGER.GETCONNECTION ("JDBC: ODBC: Pubs", "SA", "" );

You are already very familiar with the statement, but what is the database? :-)

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 It is ok, but we can go deep into some things to do this, you can learn the programming mode (such as Interface mode, etc.) 1 Class.Forname (String classname) source code is: Public FinalClass Class Implements Java.io.serializable {... public static class forname (String classname) throws classnotfoundexception {return, true, classloader.getCallerclassLoader ());}

...} About Forname0, please check your own JDK Source.

The purpose is to load the specified Class to the JVM. (Note Class Loading, Initialization Process) The STATIC block that is loaded (below) will be performed during the loading process.

2 sun's JdbcOdbcDriver source: public class JdbcOdbcDriver extends JdbcOdbcObject implements 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 {return null (initialize ()!);} else {JdbcOdbcConnection jdbcodbcconnection = new JdbcOdbcConnection (OdbcApi, hEnv, this); jdbcodbcconnection.initialize (getSubName (s), properties, DriverManager.getLoginTimeout ()); jdbcodbcconnection.setURL ( s); return jdbcodbcConnection;}} static {if (jdbcodbcObject.istic ()) JDBCODBCOBJECT.TRACE ("JDBCODB cDriver class loaded "); JdbcOdbcDriver jdbcodbcdriver = new JdbcOdbcDriver (); try {DriverManager.registerDriver (jdbcodbcdriver);} catch (SQLException sqlexception) {if (JdbcOdbcObject.isTracing ()) 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);}

return (getConnection (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 Structure Diagram: Please click View Structure Figure http://jdeveloper.myrice.com/doc/jdbc/images/howjdbc.jpg

Please go to http://jdeveloper.myrice.com/ to see more java articles

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

New Post(0)