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

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

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

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

    Posted on 2006-03-02 20:57 killvin 閱讀(348) 評(píng)論(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.

    主站蜘蛛池模板: 国产 亚洲 中文在线 字幕| avtt天堂网手机版亚洲| 青青青视频免费观看| 四虎成人精品一区二区免费网站| 美女被爆羞羞网站免费| 国产精品亚洲综合一区在线观看 | 亚洲电影免费观看| 亚洲欧洲一区二区三区| 男人免费视频一区二区在线观看 | 久久久久久免费视频| 国产无遮挡吃胸膜奶免费看视频| 免费在线看黄的网站| 午夜精品射精入后重之免费观看| 亚洲大码熟女在线观看| 一级毛片一级毛片免费毛片| 色欲A∨无码蜜臀AV免费播 | 国产AV无码专区亚洲AV手机麻豆| 99亚洲精品高清一二区| 国产va在线观看免费| 又色又污又黄无遮挡的免费视| 亚洲第一精品福利| 在免费jizzjizz在线播| 亚洲第一成年免费网站| 亚洲日本韩国在线| 亚洲综合激情五月色一区| 日本一线a视频免费观看| 一级一级毛片免费播放| 夜夜亚洲天天久久| 天天看片天天爽_免费播放| 日本特黄特色AAA大片免费| 国产亚洲福利精品一区| 日韩大片免费观看视频播放| 国产亚洲一区二区手机在线观看 | av无码国产在线看免费网站 | 亚洲AV无码成人网站在线观看| 一级看片免费视频| 亚洲一区中文字幕久久| www.av在线免费观看| 亚洲视频在线视频| 国产精品免费_区二区三区观看| 亚洲国产成人91精品|