<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 閱讀(353) 評論(0)  編輯  收藏 所屬分類: Java

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類(42)

    隨筆檔案(43)

    文章分類

    相冊

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲精品久久久www| 女人18毛片a级毛片免费| 欧洲美熟女乱又伦免费视频 | 一级毛片a免费播放王色电影 | 亚洲日韩精品国产一区二区三区 | 亚洲精品亚洲人成人网| 亚洲免费观看视频| 免费看无码自慰一区二区| 最新亚洲精品国偷自产在线| 亚洲毛片在线免费观看| 亚洲精品日韩中文字幕久久久| 最近中文字幕高清免费中文字幕mv| 亚洲专区在线视频| 一级中文字幕乱码免费| 亚洲色精品vr一区二区三区| 伊人免费在线观看高清版| 日本一道在线日本一道高清不卡免费| 亚洲日本一区二区三区| 亚洲视频免费在线播放| 色婷五月综激情亚洲综合| 卡1卡2卡3卡4卡5免费视频| 另类专区另类专区亚洲| 国产亚洲自拍一区| 色一情一乱一伦一视频免费看| 99久久久精品免费观看国产| 波多野结衣亚洲一级| 国产又黄又爽又刺激的免费网址| 免费夜色污私人影院网站电影 | 日本免费人成在线网站| 亚洲精华液一二三产区| 亚洲综合亚洲综合网成人| 国产精品免费无遮挡无码永久视频 | 久久精品国产亚洲精品| 久久久久国产精品免费网站| 亚洲一级黄色视频| 免费国产99久久久香蕉| 亚洲熟妇久久精品| 成年女人喷潮毛片免费播放| 曰批全过程免费视频免费看| 亚洲第一福利网站| 777爽死你无码免费看一二区|