国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

圖解Java連接數(shù)據(jù)庫內(nèi)幕

2019-11-18 12:10:24
字體:
供稿:網(wǎng)友

  java.sql包中的 java.sql.Driver, jdbc.sql.Connection等提供給程序開發(fā)人員統(tǒng)一的開發(fā)接口
  數(shù)據(jù)庫提供商提供相應(yīng)的實現(xiàn),對程序開發(fā)人員來講只要知道這些接口都有哪些方法就可以了
  但我們可以深入一些 看看到底這里面都做了那些事, 同時也可以學習其中的編程模式(如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());
  }
  
  ...
  }
  關(guān)于forName0 請自己查看jdk source.
  
  目的是把指定的Class裝載到JVM中來。(注重class的裝載、初始化過程)
  在裝載過程中將執(zhí)行被裝載類的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 結(jié)構(gòu)圖:
  圖解Java連接數(shù)據(jù)庫內(nèi)幕

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 静海县| 龙泉市| 安图县| 武隆县| 临安市| 潮安县| 定远县| 阿拉尔市| 舟山市| 六盘水市| 阿尔山市| 青阳县| 北碚区| 梁平县| 怀远县| 青冈县| 平顺县| 六枝特区| 马鞍山市| 林芝县| 长白| 江永县| 肇东市| 桃源县| 凤凰县| 湾仔区| 平邑县| 理塘县| 宜川县| 临夏市| 吐鲁番市| 嵩明县| 吉安县| 延川县| 怀集县| 鹤庆县| 开平市| 增城市| 页游| 秭归县| 张家港市|