1.任何数据库驱动程序都提供对java.sql.Driver接口的驱动类,mysql-connector-java-5.1.39-bin.jar中Driver:
packagecom.mysql.jdbc;importjava.sql.DriverManager;importjava.sql.SQLException;public class Driver extendsNonRegisteringDriverimplementsjava.sql.Driver
{publicDriver()throwsSQLException
{
}static{try{
DriverManager.registerDriver(newDriver());
}catch(SQLException E) {throw new RuntimeException("Can't register driver!");
}
}
}
实际上Class.forName("com.mysql.jdbc.Driver")--->DriverManager.registerDriver(new Driver())。java.sql.DriverManager类是JDBC的管理层,负责管理JDBC驱动程序的基本服务。在DriverManager中Driver被包装为DriverInfo类,存储在:
private final static CopyOnWriteArrayList registeredDrivers = new CopyOnWriteArrayList<>();
2.Connection con= DriverManager.getConnection(url,"user","pass");遍历存储在registeredDrivers中的DriverInfo,调用各自的connect()。
for(DriverInfo aDriver : registeredDrivers) {//If the caller does not have permission to load the driver then//skip it.
if(isDriverAllowed(aDriver.driver, callerCL)) {try{
println(" trying " +aDriver.driver.getClass().getName());
Connection con=aDriver.driver.connect(url, info);if (con != null) {//Success!
println("getConnection returning " +aDriver.driver.getClass().getName());return(con);
}
}catch(SQLException ex) {if (reason == null) {
reason=ex;
}
}
}else{
println(" skipping: " +aDriver.getClass().getName());
}
}
3.com.mysql.jdbc.Driver继承NonRegisteringDriver,实际的connect在com.mysql.jdbc.NonRegisteringDriver.connect()中实现,方法返回类型为java.sql.Connection。
publicjava.sql.Connection connect(String url, Properties info)throwsSQLException
{if (url == null) {throw SQLError.createSQLException(Messages.getString("NonRegisteringDriver.1"), "08001", null);
}if (StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:loadbalance://"))returnconnectLoadBalanced(url, info);if (StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:replication://")) {returnconnectReplicationConnection(url, info);
}
Properties props= null;if ((props = parseURL(url, info)) == null) {return null;
}if (!"1".equals(props.getProperty("NUM_HOSTS"))) {returnconnectFailover(url, info);
}try{returnConnectionImpl.getInstance(host(props), port(props), props, database(props), url);
}catch(SQLException sqlEx)
{throwsqlEx;
}catch(Exception ex) {
SQLException sqlEx= SQLError.createSQLException(Messages.getString("NonRegisteringDriver.17") + ex.toString() + Messages.getString("NonRegisteringDriver.18"), "08001", null);
sqlEx.initCause(ex);throwsqlEx;
}
}
4.那么mysql-connector-java-5.1.39-bin.jar中具体是怎么执行sql的,怎么学习,有哪些书可以看呢???