简体   繁体   English

如果在使用JDBC时不包括数据库驱动程序,会发生什么情况?

[英]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. 我是java开发的新手,很高兴看到与支持php的环境相比,支持多个平台的数据库实现要容易得多。

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(); 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? 但是,忽略这一点似乎也可以正常工作-所以我的问题是,给定服务器URL,jdbc是否会自动找到驱动程序? 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! 另外-如果有人对在线Java学习资源(尤其是企业开发)有任何好的提示,请分享!

Yes, this has improved in JDK 6. See this O'Reilly article for JDBC 4.0 improvements . 是的,这在JDK 6中得到了改进有关JDBC 4.0的改进,请参见O'Reilly的这篇文章

In particular: 尤其是:

In JDBC 4.0, we no longer need to explicitly load JDBC drivers using Class.forName(). 在JDBC 4.0中,我们不再需要使用Class.forName()显式加载JDBC驱动程序。 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. 调用方法getConnection时,DriverManager将尝试从初始化时加载的JDBC驱动程序和使用与当前应用程序相同的类加载器显式加载的JDBC驱动程序中查找合适的驱动程序。

See also the JavaDoc for DriverManager in JDK 6. 另请参见JDK 6中的JavaDoc for DriverManager

这不是JDK 6中的更改吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我在使用Hashmap时没有实现Serializable会发生什么 - What happens if I don't implement Serializable when using Hashmap 将JDBC驱动程序用于DB2时发生ClassNotFoundException - ClassNotFoundException when using JDBC driver for DB2 在Oracle JDBC驱动程序中,将Java日期写入TIMESTAMP列时,时区会发生什么? - In the Oracle JDBC driver, what happens to the time zone when you write a Java date to a TIMESTAMP column? 当子类没有在Java中定义构造函数时会发生什么? - What happens when subclasses don't define a constructor in Java? 如果我不关闭 kafka 生产者会发生什么 - What happens if I don't close the kafka producer Maven:如果我不知道客户端将使用哪个数据库,如何创建正确的JDBC驱动程序依赖关系? - Maven: How to create a correct JDBC driver dependency, if I don't know which database the client will use? TigerGraph DB 的 JDBC 驱动程序 - JDBC Driver for TigerGraph DB 当JDBC AutoCommit为False并且未设置显式事务边界时会发生什么 - What happens when JDBC AutoCommit is False and explicit Transaction boundaries aren't set 如何使用JDBC驱动程序获取DB2中所有现有数据库的列表? - How can I obtain a list of all existing databases in DB2 using the JDBC driver? 我无法加载 MySQL 的 JDBC 驱动程序 - I can't load the JDBC driver for MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM