java源码阅读,看看是怎么组织起来表达逻辑的,菜鸟笔记。
1.1一阵土话
类a中有接口A作属性Field,接口A中定义了抽象方法aM,规定了返回值是接口B
调用:类a对象.接口A对象(接口实例化).aM(接口方法),返回值接口B的实现类对象(aM的返回值是对象)
1.2原java语句在这里:
- //获取数据库的连接对象
- Connection conn=DriverManager.getConnection
- ("jdbc:mysql://localhost:3306/db3","root","root");
1.3解读:DriverManager.getConnection ---DriverManager的静态方法getConnection
getConnection方法原型:
- public static Connection getConnection(String url,
- String user, String password) throws SQLException {
- java.util.Properties info = new java.util.Properties();
-
- if (user != null) {
- info.put("user", user);
- }
- if (password != null) {
- info.put("password", password);
- }
-
- return (getConnection(url, info, Reflection.getCallerClass()));
- }
查看return内容: return (getConnection(url, info, Reflection.getCallerClass()));
继续查看getConnection()方法
- private static Connection getConnection(
- String url, java.util.Properties info, Class> caller) throws SQLException {
- /*
- * When callerCl is null, we should check the application's
- * (which is invoking this class indirectly)
- * classloader, so that the JDBC driver class outside rt.jar
- * can be loaded from here.
- */
- ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
- synchronized(DriverManager.class) {
- // synchronize loading of the correct classloader.
- if (callerCL == null) {
- callerCL = Thread.currentThread().getContextClassLoader();
- }
- }
-
- if(url == null) {
- throw new SQLException("The url cannot be null", "08001");
- }
-
- println("DriverManager.getConnection(\"" + url + "\")");
-
- // Walk through the loaded registeredDrivers attempting to make a connection.
- // Remember the first exception that gets raised so we can reraise it.
- SQLException reason = null;
-
- 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());
- }
-
- }
-
- // if we got here nobody could connect.
- if (reason != null) {
- println("getConnection failed: " + reason);
- throw reason;
- }
-
- println("getConnection: no suitable driver found for "+ url);
- throw new SQLException("No suitable driver found for "+ url, "08001");
- }
找到返回值项return(con);
- 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());
- }
-
- }
其中
Connection con = aDriver.driver.connect(url, info);
就是需要的结果,而这一段可以衔接上1.1,分析如下:
DriverInfo类,包含了接口Drive
- class DriverInfo {
-
- final Driver driver;
- DriverAction da;
- DriverInfo(Driver driver, DriverAction action) {
- this.driver = driver;
- da = action;
- }
接口里有抽象方法connect,返回值是接口对象connection
- public interface Driver {
-
-
- Connection connect(String url, java.util.Properties info)
- throws SQLException;
aDriver是DriverInfo对象,driver是Driver接口类型的属性,connect是Driver接口方法
- 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);
- }
如此可以返回一个Connection接口的实现类对象
小结:java里常用接口来实现目的,而不一定在意其具体实现类是什么。
Java里数据传递基本是以对象进行的。