Tomcat用mysql:“无法创建类''的连接URL的JDBC驱动程序'null'" [英] Tomcat with mysql : " Cannot create JDBC driver of class '' for connect URL 'null' "

查看:139
本文介绍了Tomcat用mysql:“无法创建类''的连接URL的JDBC驱动程序'null'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已被多次回答,但仍然无法解决我的问题。



Host OS是Fedora 17



Tomcat程序包tomcat-7.0.27-2.fc17.noarch



这是我的$ CATALINA_HOME / logs / catalina.out

  [DEBUG]:无法与数据存储建立连接
org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法创建JDBC驱动程序的org.apache.tomcat.dbcp.dbcp中的连接URL为null
的类''''的类'''
。 BasicDataSource.createDataSource(BasicDataSource.java:1371)
在org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
...
...
导致:java.sql.SQLException:没有合适的驱动程序
at java.sql.DriverManager.getDriver(DriverManager.java:289)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource .createConnectionFactory(BasicDataSource.java:1437)
...

这是我的$ CATALINA_HOME /conf/Catalina/localhost/ABC/ABC.xml

 < Context path =/ ABC> 

<! - maxActive:池中最大dB连接数。确保你
配置你的mysqld max_connections足够大,以处理
所有的数据库连接。设置为-1表示无限制。
- >

<! - maxIdle:要在池中保留的空闲dB连接的最大数量。
设置为-1表示没有限制。另请参见关于此
和minEvictableIdleTimeMillis配置参数的DBCP文档。
- >

<! - maxWait:等待dB连接可用的最大时间
(以ms为单位),在本例中为10秒。如果超过
此超时,则抛出异常。设置为-1即可无限期等待。
- >

<! - 用户名和密码:DB用户名和KeyDB密码密钥(不是密码本身) - >

<! - driverClassName:JDBC KeyDB包装器的类名。这些包装器
用来自KeyDB的相应值替换mysql_keydb_key
- >

<! - url:用于连接MySQL db的JDBC连接URL。
url的autoReconnect = true参数确保如果mysqld关闭
连接,
mm.mysql JDBC驱动程序将自动重新连接。 mysqld默认情况下在8小时后关闭空闲连接。
- >

< Resource name =jdbc / last_dbauth =Containertype =javax.sql.DataSourcemaxActive =100maxIdle =30maxWait =10000username = ****password =****driverClassName =com.mysql.jdbc.Driverurl =jdbc:mysql:// localhost:3306 / last_db/>
< / Context>

这是我的$ CATALINA_HOME / lib目录

  $ ls / usr / share / tomcat / lib 
annotations-api.jar commons-collections.jar log4j.jar tomcat-i18n-es.jar tomcat-servlet-3.0 -api.jar
apache-commons-dbcp.jar commons-dbcp.jar mysql-connector-java-5.1.0.jar tomcat-i18n-fr.jar tomcat-util.jar
catalina-ant .jar commons-logging-1.1.1.jar tomcat-api.jar tomcat-i18n-ja.jar
catalina-ha.jar jasper-el.jar tomcat-coyote.jar tomcat-jdbc.jar
catalina.jar jasper.jar tomcat-dbcp.jar tomcat-jsp-2.2-api.jar
catalina-tribes.jar jasper-jdt.jar tomcat-el-2.2-api.jar tomcat-juli.jar

这是我的WEB-INF / web.xml

 <?xml version =1.0encoding =UTF-8?> 
< web-app xmlns =http://java.sun.com/xml/ns/javaee
xmlns:xsi =http://www.w3.org/2001/XMLSchema -instance
xsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
version =2.5>

< servlet>
< servlet-name> jersey< / servlet-name>
< servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name> jersey< / servlet-name>
< url-pattern> / v1 / *< / url-pattern>
< / servlet-mapping>
< resource-ref>
< description> DB连接< / description>
< res-ref-name> jdbc / last_db< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< / resource-ref>

< / web-app>

执行代码:

  try {
Context initContext = new InitialContext();
上下文envContext =(Context)initContext.lookup(java:/ comp / env);
dataSource =(DataSource)envContext.lookup(jdbc /+ dbName);
} catch(NamingException ex){
System.out.println([DEBUG]:Failed to initiate JDBC context);
ex.printStackTrace();
return;
}

try {
connection = dataSource.getConnection();
} catch(SQLException ex){
System.out.println([DEBUG]:无法与数据存储建立连接);
ex.printStackTrace();
return;
} catch(ClassNotFundException ww){
ww.printStackTrace();
}

mysql连接器jar似乎在WEB-INF / lib

  $ jar tf ABC.war | grep mysql-connector 
WEB-INF / lib / mysql-connector-java-5.1.0.jar


$ b b

我不知道这是否有用,但这里是3306端口的侦听端口信息

  $ netstat -tln 
活动Internet连接(仅服务器)
Proto Recv-Q Send-Q本地地址外部地址状态
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN


解决方案

我觉得在桌子上撞我的头。 >

问题是,不是保留ABC.xml
$ CATALINA_HOME / conf / Catalina / localhost / ABC / ABC.xml
它应该是保存在这里:
$ CATALINA_HOME / conf / Catalina / localhost / ABC.xml



StackTrace对连接器驱动程序的错误非常具有误导性。


This question has been asked and answered multiple times but it still doesn't solve my problem.

Host OS is Fedora 17

Tomcat package tomcat-7.0.27-2.fc17.noarch

Here is my $CATALINA_HOME/logs/catalina.out

[DEBUG]: Failed to establish connection with datastore
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
...
...
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:289)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
...

Here is my $CATALINA_HOME/conf/Catalina/localhost/ABC/ABC.xml

<Context path="/ABC">

<!-- maxActive: 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 -1 for no limit.
     -->

<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
     Set to -1 for no limit.  See also the DBCP documentation on this
     and the minEvictableIdleTimeMillis configuration parameter.
     -->

<!-- maxWait: Maximum time to wait for a dB connection to become available
     in ms, in this example 10 seconds. An Exception is thrown if
     this timeout is exceeded.  Set to -1 to wait indefinitely.
     -->

<!-- username and password: DB username and KeyDB password key (not the password itself)  -->

<!-- driverClassName: Class name for the JDBC KeyDB wrappers.  These wrappers
     replace the 'mysql_keydb_key' with its corresponding value from KeyDB
     -->

<!-- url: 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.
     -->

<Resource name="jdbc/last_db" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="****" password="****" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/last_db"/>
</Context>

Here is my $CATALINA_HOME/lib directory

$ ls /usr/share/tomcat/lib
annotations-api.jar      commons-collections.jar    log4j.jar                          tomcat-i18n-es.jar      tomcat-servlet-3.0-api.jar
apache-commons-dbcp.jar  commons-dbcp.jar           mysql-connector-java-5.1.0.jar  tomcat-i18n-fr.jar      tomcat-util.jar
catalina-ant.jar         commons-logging-1.1.1.jar  tomcat-api.jar                  tomcat-i18n-ja.jar
catalina-ha.jar          jasper-el.jar              tomcat-coyote.jar               tomcat-jdbc.jar
catalina.jar             jasper.jar                 tomcat-dbcp.jar                 tomcat-jsp-2.2-api.jar
catalina-tribes.jar      jasper-jdt.jar             tomcat-el-2.2-api.jar           tomcat-juli.jar

Here is my WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<servlet>
    <servlet-name>jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/v1/*</url-pattern>
</servlet-mapping>
<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/last_db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

</web-app>

Executing code:

try {
    Context initContext = new InitialContext();
    Context envContext  = (Context)initContext.lookup("java:/comp/env");
    dataSource = (DataSource) envContext.lookup("jdbc/"+dbName);
 } catch (NamingException ex) {
    System.out.println("[DEBUG]: Failed to initiate JDBC context");
    ex.printStackTrace();
    return;
 }

 try {
     connection = dataSource.getConnection();
 } catch (SQLException ex){
     System.out.println("[DEBUG]: Failed to establish connection with datastore");
     ex.printStackTrace();
     return;
 } catch (ClassNotFoundException ww){
     ww.printStackTrace();
 }

The mysql-connector jar seems to be available in WEB-INF/lib

$ jar tf ABC.war | grep mysql-connector
WEB-INF/lib/mysql-connector-java-5.1.0.jar

I don't know if this is useful or not but here is the listening port information for port 3306

$ netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN     

解决方案

I feel like banging my head on the table.

The problem was that instead of keeping the ABC.xml $CATALINA_HOME/conf/Catalina/localhost/ABC/ABC.xml It is supposed to be kept here: $CATALINA_HOME/conf/Catalina/localhost/ABC.xml

The StackTrace was very misleading regarding error with the connector driver.

这篇关于Tomcat用mysql:“无法创建类''的连接URL的JDBC驱动程序'null'&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆