今天使用Tomcat连接数据库时出现了Cannot create JDBC driver of class '' for connect URL 'null' 错误,程序是没有问题的。 最后查找发现是Tomcat版本的问题,原来的版本是Tomcat5.0,现在是5.5,二者server.xml中对数据源的配置发生了一些变化。
Tomcat5.0中:
<Resource name="jdbc/myapp"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/myapp">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>30</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 15 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>15000</value>
</parameter>
<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter>
<!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/myapp?autoReconnect=true</value>
</parameter>
</ResourceParams>
Tomcat5.5中:
<Resource name="jdbc/myapp"
type="javax.sql.DataSource"
password="root"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="30"
maxWait="15000"
username="root"
url="jdbc:mysql://localhost:3306/myapp?autoReconnect=true"
maxActive="30"/>
重启Tomcat,提示消失。
这个错误的出现可能还有其他很多种的原因,这个只是其中一个。