• Java源码阅读


    前言:

    java源码阅读,看看是怎么组织起来表达逻辑的,菜鸟笔记。

    1.connection接口作数据库连接对象

    1.1一阵土话

    类a中有接口A作属性Field,接口A中定义了抽象方法aM,规定了返回值是接口B

    调用:类a对象.接口A对象(接口实例化).aM(接口方法),返回值接口B的实现类对象(aM的返回值是对象)

    1.2原java语句在这里:

    1. //获取数据库的连接对象
    2. Connection conn=DriverManager.getConnection
    3. ("jdbc:mysql://localhost:3306/db3","root","root");
     
    

    1.3解读:DriverManager.getConnection ---DriverManager的静态方法getConnection

    getConnection方法原型:

    1. public static Connection getConnection(String url,
    2. String user, String password) throws SQLException {
    3. java.util.Properties info = new java.util.Properties();
    4. if (user != null) {
    5. info.put("user", user);
    6. }
    7. if (password != null) {
    8. info.put("password", password);
    9. }
    10. return (getConnection(url, info, Reflection.getCallerClass()));
    11. }

    查看return内容: return (getConnection(url, info, Reflection.getCallerClass()));

    继续查看getConnection()方法
    1. private static Connection getConnection(
    2. String url, java.util.Properties info, Class caller) throws SQLException {
    3. /*
    4. * When callerCl is null, we should check the application's
    5. * (which is invoking this class indirectly)
    6. * classloader, so that the JDBC driver class outside rt.jar
    7. * can be loaded from here.
    8. */
    9. ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
    10. synchronized(DriverManager.class) {
    11. // synchronize loading of the correct classloader.
    12. if (callerCL == null) {
    13. callerCL = Thread.currentThread().getContextClassLoader();
    14. }
    15. }
    16. if(url == null) {
    17. throw new SQLException("The url cannot be null", "08001");
    18. }
    19. println("DriverManager.getConnection(\"" + url + "\")");
    20. // Walk through the loaded registeredDrivers attempting to make a connection.
    21. // Remember the first exception that gets raised so we can reraise it.
    22. SQLException reason = null;
    23. for(DriverInfo aDriver : registeredDrivers) {
    24. // If the caller does not have permission to load the driver then
    25. // skip it.
    26. if(isDriverAllowed(aDriver.driver, callerCL)) {
    27. try {
    28. println(" trying " + aDriver.driver.getClass().getName());
    29. Connection con = aDriver.driver.connect(url, info);
    30. if (con != null) {
    31. // Success!
    32. println("getConnection returning " + aDriver.driver.getClass().getName());
    33. return (con);
    34. }
    35. } catch (SQLException ex) {
    36. if (reason == null) {
    37. reason = ex;
    38. }
    39. }
    40. } else {
    41. println(" skipping: " + aDriver.getClass().getName());
    42. }
    43. }
    44. // if we got here nobody could connect.
    45. if (reason != null) {
    46. println("getConnection failed: " + reason);
    47. throw reason;
    48. }
    49. println("getConnection: no suitable driver found for "+ url);
    50. throw new SQLException("No suitable driver found for "+ url, "08001");
    51. }

    找到返回值项return(con);

    1. for(DriverInfo aDriver : registeredDrivers) {
    2. // If the caller does not have permission to load the driver then
    3. // skip it.
    4. if(isDriverAllowed(aDriver.driver, callerCL)) {
    5. try {
    6. println(" trying " + aDriver.driver.getClass().getName());
    7. Connection con = aDriver.driver.connect(url, info);
    8. if (con != null) {
    9. // Success!
    10. println("getConnection returning " + aDriver.driver.getClass().getName());
    11. return (con);
    12. }
    13. } catch (SQLException ex) {
    14. if (reason == null) {
    15. reason = ex;
    16. }
    17. }
    18. } else {
    19. println(" skipping: " + aDriver.getClass().getName());
    20. }
    21. }

    其中

     Connection con = aDriver.driver.connect(url, info);

    就是需要的结果,而这一段可以衔接上1.1,分析如下:

    DriverInfo类,包含了接口Drive

    1. class DriverInfo {
    2. final Driver driver;
    3. DriverAction da;
    4. DriverInfo(Driver driver, DriverAction action) {
    5. this.driver = driver;
    6. da = action;
    7. }

     接口里有抽象方法connect,返回值是接口对象connection

    1. public interface Driver {
    2. Connection connect(String url, java.util.Properties info)
    3. throws SQLException;

    aDriver是DriverInfo对象,driver是Driver接口类型的属性,connect是Driver接口方法

    1. for(DriverInfo aDriver : registeredDrivers) {
    2. // If the caller does not have permission to load the driver then
    3. // skip it.
    4. if(isDriverAllowed(aDriver.driver, callerCL)) {
    5. try {
    6. println(" trying " + aDriver.driver.getClass().getName());
    7. Connection con = aDriver.driver.connect(url, info);
    8. if (con != null) {
    9. // Success!
    10. println("getConnection returning " + aDriver.driver.getClass().getName());
    11. return (con);
    12. }

    如此可以返回一个Connection接口的实现类对象

    小结:java里常用接口来实现目的,而不一定在意其具体实现类是什么。

                Java里数据传递基本是以对象进行的。

  • 相关阅读:
    oracle锁表
    Python中Tkinter模块的Canvas控件使用学习(3:绘制工艺卡片)
    Python:__call__和__getattr__组合黑魔法
    博弈论入门
    设计模式常见面试题
    【小嘟陪你刷题03】牛客网——Java专项练习
    Mybatis解决多条件查询不能得到正确SQL的两种方法 恒等式 where标签
    c++ SQLite 特别好用的库使用实例-更新(3)
    机器学习经验
    注意!JAVA中的值传递
  • 原文地址:https://blog.csdn.net/jllws1/article/details/126908133