简体   繁体   中英

What happens if I don't include a db driver when using JDBC?

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!

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.

这不是JDK 6中的更改吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM