<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Aaron Johnson對Class.forName()的解釋!

    Posted on 2006-03-02 20:57 killvin 閱讀(341) 評論(0)  編輯  收藏 所屬分類: java

    A reader asked a question via a comment a couple months ago that I didn't really have an answer for (and had always kind of wondered the same thing). In the original post (which showed how to use JDBC with ColdFusion), I used the following snippet of code:

    Class.forName("jdbc.DriverXYZ");
    Connection con = DriverManager.getConnection(url,
      "myLogin", "myPassword");

    and the reader wanted to know what the Class.forName(..) method did. The most common answer you'll hear is that it loads the database driver, which, while technically true, is shallow. Where does it get loaded? How does it happen? And why?

    To answer the question I started with the JavaDoc for the Class.forName() method. According to the documentation, the method:

    ... attempts to locate, load, and link the class or interface
    I wasn't perfectly clear on what "locate, load, and link" meant, so I did a little digging through the Java Language Specification. According to chapter 12 of the JLS:
    Loading refers to the process of finding the binary form of a class or interface type with a particular name, perhaps by computing it on the fly, but more typically by retrieving a binary representation previously computed from source code by a compiler, and constructing, from that binary form, a Class object to represent the class or interface.
    Next, again according to the JLS, it must be transformed from it's binary representation to something the Java virtual machine can use, this process is called linking. Finally, the class is initialized, which is the process that executes the static initializer and the initializers for static fields declared in the class.

    So then back to the original problem, when Class.forName() is called with an argument like this:

    Class.forName("org.gjt.mm.mysql.Driver");

    the classloader attempts to load and link the Driver class in the "org.gjt.mm.mysql" package and if successful, the static initializer is run. The MySQL Driver (download the source code) static initializer looks like this:

    static {
      try {
        java.sql.DriverManager.registerDriver(new Driver());
      } catch (SQLException E) {
        throw new RuntimeException("Can't register driver!");
      }
    }

    So it calls a static method in the java.sql.DriverManager class which apparently registers a copy of itself when it loads.

    So now I understand the where and the how, what about why? To understand the why you have to look at the next line in the initial code example:

    Connection con = DriverManager.getConnection(url,
      "myLogin", "myPassword");

    The DriverManager class (view DriverManager source here) returns a database connection given a JDBC URL string, a username and a password. In order to create that connection, the DriverManager class has to know which database driver you want to use. It does that by iterating over the array (internally a Vector) of drivers that have registered with it (ie: the registerDriver(Driver driver) method illustrated above) and calls the acceptsURL(url)) method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL.

    So there you have it. Class.forName explained.

    主站蜘蛛池模板: 精品国产亚洲一区二区三区| 免费精品国产日韩热久久| 成人免费视频小说| 亚洲高清资源在线观看| WWW国产成人免费观看视频| 国产成人免费高清在线观看| 亚洲a级片在线观看| 青娱乐免费视频在线观看| 亚洲视频在线观看一区| 插鸡网站在线播放免费观看| 免费国产小视频在线观看| 亚洲日产乱码一二三区别| 欧美a级在线现免费观看| 亚洲精品电影天堂网| 免费人成网站在线观看不卡| 怡红院亚洲怡红院首页| 一级毛片a免费播放王色| 国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金 | 免费一级毛suv好看的国产网站| 亚洲国产成人精品久久久国产成人一区二区三区综 | 曰批全过程免费视频播放网站| 亚洲视频免费观看| 日韩免费观看一级毛片看看| 男人免费视频一区二区在线观看 | 亚洲最大视频网站| 麻豆国产VA免费精品高清在线 | 免费无码AV一区二区| 亚洲AV乱码一区二区三区林ゆな | 亚洲成年人电影网站| 最近最新的免费中文字幕| 国产精品亚洲а∨天堂2021| 国产成人亚洲精品狼色在线 | 1000部禁片黄的免费看 | igao激情在线视频免费| 亚洲人成影院在线| 国产极品美女高潮抽搐免费网站| 人体大胆做受免费视频| 亚洲毛片一级带毛片基地| 国产在线a不卡免费视频| 国偷自产一区二区免费视频| 亚洲一区二区三区高清不卡 |