<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.

    主站蜘蛛池模板: 亚洲国产精品无码久久久不卡| 成人在线免费观看| 在线看片免费人成视频播| 成人毛片免费观看视频在线 | 亚洲电影日韩精品| 亚洲一区二区视频在线观看| 国产精品亚洲一区二区三区久久 | 自拍偷自拍亚洲精品播放| 在线免费观看中文字幕| 国产精品亚洲美女久久久| 国产亚洲综合久久| 亚洲精品乱码久久久久久蜜桃| 成人嫩草影院免费观看| 久久狠狠躁免费观看| 亚洲国产成人久久综合一| 91高清免费国产自产拍2021| 亚洲第一二三四区| 99久久国产热无码精品免费 | 韩国日本好看电影免费看| 在线亚洲v日韩v| 国产亚洲AV夜间福利香蕉149| 国产在线观看无码免费视频| 免费看韩国黄a片在线观看| 亚洲精品理论电影在线观看| 在线观看黄片免费入口不卡| 免费v片在线观看| 中文字幕一区二区三区免费视频| 亚洲国产二区三区久久| 免费无码精品黄AV电影| 菠萝菠萝蜜在线免费视频| 男女交性永久免费视频播放| 青青青视频免费观看| 亚洲AV无码乱码在线观看裸奔| 国产亚洲情侣久久精品| 亚洲线精品一区二区三区影音先锋| 男人j进入女人j内部免费网站| 亚洲人成免费电影| 美丽姑娘免费观看在线观看中文版 | 亚洲国产综合久久天堂| 日韩精品免费视频| 亚洲大码熟女在线观看|