如何修复:"使用池时没有找到适合jdbc:mysql:// localhost/dbname的驱动程序"错误?

Tam*_*mer 92 mysql jdbc tomcat7 apache-commons-dbcp

我正在尝试创建与我的数据库的连接,当我使用main方法测试我的代码时,它可以无缝地工作.但是,当尝试通过Tomcat 7访问它时,它会失败并显示错误:

No suitable driver found for jdbc:mysql://localhost/dbname. 
Run Code Online (Sandbox Code Playgroud)

我正在使用汇集.我在WEB-INF/lib和.classpath中放入了mysql连接器(5.1.15),dbcp(1.4)和pool(1.4.5)库.我正在使用Eclipse IDE.我的数据库驱动程序代码是:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.tomcat.dbcp.dbcp.ConnectionFactory;
import org.apache.tomcat.dbcp.dbcp.DriverManagerConnectionFactory;
import org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory;
import org.apache.tomcat.dbcp.dbcp.PoolingDriver;
import org.apache.tomcat.dbcp.pool.impl.GenericObjectPool;

public class DatabaseConnector {
    public static String DB_URI = "jdbc:mysql://localhost/dbname";
    public static String DB_USER = "test";
    public static String DB_PASS = "password";

    // Singleton instance
    protected static DatabaseConnector _instance;

    protected String _uri;
    protected String _username;
    protected String _password;

    /**
     * Singleton, so no public constructor
     */
    protected DatabaseConnector(String uri, String username, String password) {
        _uri = uri;
        _username = username;
        _password = password;

        GenericObjectPool connectionPool = new GenericObjectPool(null);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            _uri, _username, _password);
        PoolableConnectionFactory poolableConnectionFactory =
            new PoolableConnectionFactory(connectionFactory, connectionPool,
                                            null, null, false, true);
        PoolingDriver driver = new PoolingDriver();
        driver.registerPool("test", connectionPool);
    }

    /**
     * Returns the singleton instance
     */
    public static DatabaseConnector getInstance() {
        if (_instance == null) {
            _instance = new DatabaseConnector(DB_URI, DB_USER, DB_PASS);
        }
        return _instance;
    }

    /**
     * Returns a connection to the database
     */
    public Connection getConnection() {
        Connection con = null;
        try {
            con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return con;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的堆栈跟踪开始:

Apr 5, 2011 9:49:14 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Login] in context with path [/Project] 
threw exception
java.lang.RuntimeException: java.sql.SQLException: 
No suitable driver found for jdbc:mysql://localhost/dbname
Run Code Online (Sandbox Code Playgroud)

导致此错误的原因是什么?

unc*_*ons 56

尝试将驱动程序jar放在服务器lib文件夹中.($ CATALINA_HOME/lib中)

我相信甚至在实例化应用程序之前就需要设置连接池.(至少这是它在Jboss中的工作方式)


Eri*_*ski 43

你得到这个错误的原因:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/dbname
Run Code Online (Sandbox Code Playgroud)

是因为你忘了用java应用程序注册你的mysql jdbc驱动程序.

这就是你写的:

Connection con = null;
try {
    con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
} catch (SQLException e) {
    throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

Connection con = null;
try {
    //registering the jdbc driver here, your string to use 
    //here depends on what driver you are using.
    Class.forName("something.jdbc.driver.YourFubarDriver");   
    con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
} catch (SQLException e) {
    throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)

您必须阅读特定mysql jdbc驱动程序的手册,以找到放在Class.forName("...")参数中的确切字符串.

JDBC v.4不需要Class.forName

Class.forName("something.jdbc.driver.YourFubarDriver")如果使用最新的(JDBC v.4)驱动程序,则不再需要从Java 6开始.有关详细信息,请阅读:http://onjava.com/pub/a/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html

  • 根据http://tomcat.apache.org/tomcat-8.0-doc/jndi-datasource-examples-howto.html#DriverManager,_the_service_provider_mechanism_and_memory_leaks"在其WEB-INF/lib目录中具有数据库驱动程序的Web应用程序不能依赖于服务提供商机制,并应明确注册驱动程序." (12认同)
  • 我发现使用Tomcat 7和Java 7再次需要执行Class.forName("something.jdbc.driver.YourFubarDriver"); 至少这在一次部署中为我解决了这个问题.同样的.war在使用Java 6的Tomcat 5.5上运行良好,但在7/7上抛出了不合适的驱动程序异常. (5认同)
  • 你不应该需要它们.但是,它高度依赖于驱动程序版本/ OS版本/ Tomcat版本/ Java版本.即使是"最近的"驱动程序和JDK组合也可能会失败.为了安全起见,您需要使用Class.forName (3认同)
  • 不,从Java 6开始,不再需要Class.forName("something.jdbc.driver.YourFubarDriver")**如果**使用最新的(JDBC v.4)驱动程序.可能与Tomcat 7一起使用旧驱动程序而不是Tomcat 5.5.有关详细信息,请参阅DriverManager的Javadoc. (2认同)
  • 我很困惑,为什么有必要调用“Class.forName”?这个类不应该在类路径中吗?为什么我们需要手动“加载”一个类?我从来不需要手动加载一个类,这个有什么特别之处? (2认同)

ybe*_*ira 37

使用Tomcat7和mysql-connector-java-5.1.26我遇到了同样的问题,我把它放在我的$ CATALINA_HOME/lib和WEB-INF/lib中,以防万一.但是在得到连接之前我使用这两个语句中的任何一个之前它都找不到它:

DriverManager.registerDriver(new com.mysql.jdbc.Driver ());

要么

Class.forName("com.mysql.jdbc.Driver");

然后我从$ CATALINA_HOME/lib中删除mysql-connector-java-5.1.26,连接仍然有效.

  • 你*应该*不需要它们.但是,它高度依赖于驱动程序版本/ OS版本/ Tomcat版本/ Java版本.即使是"最近的"驱动程序和JDK组合也可能会失败.为了安全起见,您需要使用Class.forName (3认同)
  • 从这个答案复制的文本:http://stackoverflow.com/a/5484287/2479087; 切勿手动调用DriverManager.registerDriver()方法.JDBC规范要求驱动程序在加载类时注册自身,并通过Class.forName()加载类.在JDBC 4中,驱动程序只能通过类路径自动加载. (2认同)

小智 8

当运行tomcat out of eclipse时它不会选择lib设置CATALINA_HOME/lib,有两种方法可以修复它.在eclipse服务器视图中双击Tomcat服务器,它将打开tomcat插件配置,然后:

  1. 单击"Open Launch Config">"Classpath"选项卡设置mysql连接器/ j jar位置.要么
  2. 服务器位置>选择选项,显示"使用Tomcat安装(控制Tomcat安装)"


Tun*_*zle 6

添加来自 Maven 的工件。

 <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
 </dependency>
Run Code Online (Sandbox Code Playgroud)


non*_*ipt 5

我在 $CATALINA_HOME/lib 和 WEB-INF/lib 中都有 mysql jdbc 库,但我仍然收到此错误。我需要 Class.forName("com.mysql.jdbc.Driver"); 使其发挥作用。