日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Java連結數據庫內幕

系統 2070 0
Java.sql包中的 java.sql.Driver, jdbc.sql.Connection等提供給程序開發人員統一的開發接口,數據庫提供商提供相應的實現,對程序開發人員來講只要知道這些接口都有哪些方法就可以了。但我們可以深入一些看看到底這里面都做了那些事, 同時也可以學習其中的編程模式(如Interface模式等)。
  1、 Class.forName(String classname) 的源碼為:

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

  關于forName0 請自己查看jdk source.

  的是把指定的Class裝載到JVM中來。(注意class的裝載、初始化過程)在裝載過程中將執行被裝載類的static塊(如下)

  2 sun的JdbcOdbcDriver 源碼:

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(!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 sqlexception)
   {
    if(JdbcOdbcObject.isTracing())
     JdbcOdbcObject.trace("Unable to register driver");
   }
 }
}
public interface JdbcOdbcDriverInterface
 extends Driver
 {
  ...
 }
3 連接過程

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 結構圖:

Java連結數據庫內幕


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 荣成市| 郸城县| 黑山县| 安远县| 龙江县| 盖州市| 怀宁县| 化州市| 新兴县| 灵宝市| 武宣县| 鄢陵县| 邵东县| 宜昌市| 贺州市| 木兰县| 高邮市| 洛隆县| 小金县| 纳雍县| 吉木萨尔县| 广元市| 乌拉特中旗| 龙南县| 扎囊县| 上虞市| 拉孜县| 沂南县| 甘洛县| 宁河县| 新宁县| 中西区| 长寿区| 新沂市| 大连市| 北辰区| 晋城| 无棣县| 古蔺县| 五家渠市| 中江县|