{"id":699,"date":"2005-07-31T21:56:54","date_gmt":"2005-08-01T01:56:54","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=699"},"modified":"2005-07-31T21:56:54","modified_gmt":"2005-08-01T01:56:54","slug":"java-classfornamestring-classname-and-jdbc","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2005\/07\/31\/java-classfornamestring-classname-and-jdbc\/","title":{"rendered":"Java Class.forName(String className) and JDBC"},"content":{"rendered":"<p>A reader asked a question via a comment a couple months ago that I didn&#8217;t really have an answer for (and had always kind of wondered the same thing). In <a href=\"http:\/\/cephas.net\/blog\/2004\/05\/10\/coldfusion_mx_and_javasql.html\">the original post<\/a> (which showed how to use JDBC with ColdFusion), I used the following snippet of code:<br \/>\n<code><br \/>\nClass.forName(\"jdbc.DriverXYZ\");<br \/>\nConnection con = DriverManager.getConnection(url,<br \/>\n&nbsp;&nbsp;\"myLogin\", \"myPassword\");<br \/>\n<\/code><br \/>\nand the reader wanted to know what the <code>Class.forName(..)<\/code> method did. The most common answer you&#8217;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?<\/p>\n<p>To answer the question I started with the <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/lang\/Class.html#forName(java.lang.String,%20boolean,%20java.lang.ClassLoader)\">JavaDoc for the Class.forName() method<\/a>.  According to the documentation, the method:<\/p>\n<blockquote><p>&#8230; attempts to locate, load, and link the class or interface<\/p><\/blockquote>\n<p>  I wasn&#8217;t perfectly clear on what &#8220;locate, load, and link&#8221; meant, so I did a little digging through the <a href=\"http:\/\/java.sun.com\/docs\/books\/jls\/\">Java Language Specification<\/a>.  According to <a href=\"http:\/\/java.sun.com\/docs\/books\/jls\/second_edition\/html\/execution.doc.html#44411\">chapter 12 of the JLS<\/a>: <\/p>\n<blockquote><p>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.<\/p><\/blockquote>\n<p> Next, again according to the JLS, it must be transformed from it&#8217;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. <\/p>\n<p>So then back to the original problem, when Class.forName() is called with an argument like this:<br \/>\n<code><br \/>\nClass.forName(\"org.gjt.mm.mysql.Driver\");<br \/>\n<\/code><br \/>\nthe classloader attempts to load and link the <code>Driver<\/code> class in the &#8220;org.gjt.mm.mysql&#8221; package and if successful, the static initializer is run. The MySQL <code>Driver<\/code> (download the <a href=\"http:\/\/dev.mysql.com\/downloads\/connector\/j\/3.1.html\">source code<\/a>) static initializer looks like this:<br \/>\n<code><br \/>\nstatic {<br \/>\n&nbsp;&nbsp;try {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;java.sql.DriverManager.registerDriver(new Driver());<br \/>\n&nbsp;&nbsp;} catch (SQLException E) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;throw new RuntimeException(\"Can't register driver!\");<br \/>\n&nbsp;&nbsp;}<br \/>\n}<br \/>\n<\/code><br \/>\nSo it calls a static method in the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/sql\/DriverManager.html\">java.sql.DriverManager<\/a> class which apparently registers a copy of itself when it loads.<\/p>\n<p>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:<br \/>\n<code><br \/>\nConnection con = DriverManager.getConnection(url,<br \/>\n&nbsp;&nbsp;\"myLogin\", \"myPassword\");<br \/>\n<\/code><br \/>\nThe <code>DriverManager<\/code> class (view DriverManager source <a href=\"http:\/\/developer.classpath.org\/doc\/java\/sql\/DriverManager-source.html\">here<\/a>) 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 <code>registerDriver(Driver driver)<\/code> method illustrated above) and calls the <code>acceptsURL(url))<\/code> method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL.   <\/p>\n<p>So there you have it.  Class.forName explained.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A reader asked a question via a comment a couple months ago that I didn&#8217;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(&#8220;jdbc.DriverXYZ&#8221;); Connection con = DriverManager.getConnection(url, &nbsp;&nbsp;&#8220;myLogin&#8221;, &#8220;myPassword&#8221;); &hellip; <a href=\"https:\/\/cephas.net\/blog\/2005\/07\/31\/java-classfornamestring-classname-and-jdbc\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java Class.forName(String className) and JDBC<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/699"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=699"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/699\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}