diff options
Diffstat (limited to '.html/java/stop-using-class-dot-forname.html')
| -rw-r--r-- | .html/java/stop-using-class-dot-forname.html | 155 |
1 files changed, 0 insertions, 155 deletions
diff --git a/.html/java/stop-using-class-dot-forname.html b/.html/java/stop-using-class-dot-forname.html deleted file mode 100644 index 85190e8..0000000 --- a/.html/java/stop-using-class-dot-forname.html +++ /dev/null @@ -1,155 +0,0 @@ -<!DOCTYPE html> -<html> -<head> - <title> - The Codex » - Stop Using Class Dot Forname - </title> - - <link - rel='stylesheet' - type='text/css' - href='http://fonts.googleapis.com/css?family=Buenard:400,700&subset=latin,latin-ext'> - <link - rel="stylesheet" - type="text/css" - href="../media/css/reset.css"> - <link - rel="stylesheet" - type="text/css" - href="../media/css/grimoire.css"> -</head> -<body> - -<div id="shell"> - - <ol id="breadcrumbs"> - - <li class="crumb-0 not-last"> - - <a href="../">index</a> - - </li> - - <li class="crumb-1 not-last"> - - <a href="./">java</a> - - </li> - - <li class="crumb-2 last"> - - stop-using-class-dot-forname - - </li> - - </ol> - - - - <div id="article"> - <h1 id="jdbc-drivers-and-classforname">JDBC Drivers and <code>Class.forName()</code></h1> -<p>The short version: stop using <code>Class.forName(driverClass)</code> to load JDBC -drivers. You don't need this, and haven't since Java 6. You arguably never -needed this.</p> -<p>This pattern appears all over the internet, and it's wrong.</p> -<h2 id="backstory">Backstory</h2> -<p>JDBC has more or less always provided two ways to set up <code>Connection</code> objects:</p> -<ol> -<li> -<p>Obtain them from a driver-provided <code>DataSource</code> class, which applications or - containers are expected to create for themselves.</p> -</li> -<li> -<p>Obtain them by passing a URL to <code>DriverManager</code>.</p> -</li> -</ol> -<p>Most people start with the latter, since it's very straightforward to use. -However, <code>DriverManager</code> needs to be able to locate <code>Driver</code> subclasses, and -the JVM doesn't permit class enumeration at runtime.</p> -<p>In the original JDBC release, <code>Driver</code> subclasses were expected to register -themselves on load, similar to</p> -<pre><code>public class ExampleDriver extends Driver { - static { - DriverManager.registerDriver(ExampleDriver.class); - } -} -</code></pre> -<p>Obviously, applications <em>can</em> force drivers to load using -<code>Class.forName(driverName)</code>, but this hasn't ever been the only way to do it. -<code>DriverManager</code> also provides <a href="https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html">a mechanism to load a set of named classes at -startup</a>, -via the <code>jdbc.drivers</code> <a href="http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html">system property</a>.</p> -<h2 id="jdbc-4-fixed-that">JDBC 4 Fixed That</h2> -<p>JDBC 4, which came out with Java 6 in the Year of our Lord <em>Two Thousand and -Six</em>, also loads drivers using the <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Service%20Provider">service -provider</a> -system, which requires no intervention at all from deployers or application -developers.</p> -<p><em>You don't need to write any code to load a JDBC 4 driver.</em></p> -<h2 id="whats-the-harm">What's The Harm?</h2> -<p>It's harmless in the immediate sense: forcing a driver to load immediately -before JDBC would load it itself has no additional side effects. However, it's -a pretty clear indicator that you've copied someone else's code without -thoroughly understanding what it does, which is a bad habit.</p> -<h2 id="but-what-about-my-database">But What About My Database?</h2> -<p>You don't need to worry about it. All of the following drivers support JDBC -4-style automatic discovery:</p> -<ul> -<li> -<p>PostgreSQL (since version 8.0-321, in 2007)</p> -</li> -<li> -<p>Firebird (since <a href="http://tracker.firebirdsql.org/browse/JDBC-140">version 2.2, in 2009</a>)</p> -</li> -<li> -<p><a href="../mysql/choose-something-else">MySQL</a> (since <a href="http://dev.mysql.com/doc/relnotes/connector-j/en/news-5-0-0.html">version 5.0, in 2005</a>)</p> -</li> -<li> -<p>H2 (since day 1, as far as I can tell)</p> -</li> -<li> -<p>Derby/JavaDB (since <a href="https://issues.apache.org/jira/browse/DERBY-930">version 10.2.1.6, in 2006</a>)</p> -</li> -<li> -<p>SQL Server (version unknown, because MSDN is archaeologically hostile)</p> -</li> -</ul> - </div> - - - -<div id="comments"> -<div id="disqus_thread"></div> -<script type="text/javascript"> - /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ - var disqus_shortname = 'grimoire'; // required: replace example with your forum shortname - - /* * * DON'T EDIT BELOW THIS LINE * * */ - (function() { - var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; - dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js'; - (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); - })(); -</script> -<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> -<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a> -</div> - - - - <div id="footer"> - <p> - - The Codex — - - Powered by <a href="http://markdoc.org/">Markdoc</a>. - -<a href="https://bitbucket.org/ojacobson/grimoire.ca/src/master/wiki/java/stop-using-class-dot-forname.md">See this page on Bitbucket</a> (<a href="https://bitbucket.org/ojacobson/grimoire.ca/history-node/master/wiki/java/stop-using-class-dot-forname.md">history</a>). - - </p> - </div> - -</div> -</body> -</html>
\ No newline at end of file |
