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

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

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

    Aaron Johnson對Class.forName()的解釋


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

    posted on 2006-11-19 20:44 Tom 閱讀(342) 評論(0)  編輯  收藏 所屬分類: Java

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類(42)

    隨筆檔案(43)

    文章分類

    相冊

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 中文字幕视频免费在线观看| 曰韩亚洲av人人夜夜澡人人爽| 真人做A免费观看| 四虎影视永久在线精品免费| 亚洲一卡2卡3卡4卡乱码 在线| 777成影片免费观看| 视频免费1区二区三区| 亚洲国产精品成人综合久久久 | 久9这里精品免费视频| 国产99久久亚洲综合精品| jlzzjlzz亚洲jzjzjz| 亚洲av无码国产精品夜色午夜| 30岁的女人韩剧免费观看| 韩国免费a级作爱片无码| 国产精品亚洲专区一区| 亚洲精品无码中文久久字幕| 精品无码一区二区三区亚洲桃色| 日本免费人成黄页在线观看视频| 久久久久久毛片免费看| 黄色毛片免费观看| 亚洲色成人网站WWW永久四虎 | 亚洲另类自拍丝袜第五页| 亚洲精品mv在线观看| 亚洲成人精品久久| 亚洲AV第一页国产精品| 亚洲人成伊人成综合网久久久| A在线观看免费网站大全| 亚洲电影免费观看| 一区二区三区在线免费看| 免费在线看黄网站| 免费人成网站在线观看不卡| 精品免费视在线观看| 久久99精品免费视频| 伊人久久免费视频| 国产免费丝袜调教视频| av无码国产在线看免费网站| 国国内清清草原免费视频99| 搡女人真爽免费视频大全| 日韩免费观看的一级毛片| 成人免费无码精品国产电影| 亚洲av高清在线观看一区二区|