views:

138

answers:

2

I'm new to java development, and was happy to see how much easier the database implementation was when it comes to supporting several platforms, compared to the php environment I'm used to.

There is, however, one thing I'm confused about - I read everywhere I have to do a runtime-include of the database driver I want to use, ie:

Class.forName(com.example.jdbc.Driver).newInstance();

However, omitting this seems to work fine too - so my question is, does jdbc find the driver, given the server url, automagically? And if so, why is this line included in all the tutorials i read on the subject?

Also - if anyone's got any good tips for online java learning resources (enterprise development in particular), please share!

+3  A: 

Didn't this change in JDK 6?

Fortyrunner
That would make a lot of sense - and would support my theory that I need to find better places to get info from :)
Jacob Hansson
+4  A: 

Yes, this has improved in JDK 6. See this O'Reilly article for JDBC 4.0 improvements.

In particular:

In JDBC 4.0, we no longer need to explicitly load JDBC drivers using Class.forName(). When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from among the JDBC drivers that were loaded at initialization and those loaded explicitly using the same class loader as the current application.

See also the JavaDoc for DriverManager in JDK 6.

Jon Skeet