如何修复:使用池时“找不到适合 jdbc:mysql://localhost/dbname 的驱动程序”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5556664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools?
提问by Tamer
I am trying to create a connection to my database, when I put test my code using the main method, it works seamlessly. However, when trying to access it through Tomcat 7, it fails with error:
我正在尝试创建到我的数据库的连接,当我使用 main 方法测试我的代码时,它可以无缝地工作。但是,当尝试通过 Tomcat 7 访问它时,它失败并显示错误:
No suitable driver found for jdbc:mysql://localhost/dbname.
I am using pooling. I put in mysql connector (5.1.15), dbcp (1.4) , and pool(1.4.5) libraries in WEB-INF/lib and in .classpath as well. I am using Eclipse IDE. My code for the database driver is:
我正在使用池化。我在 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;
}
}
Start of my stack trace:
我的堆栈跟踪的开始:
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
What is causing this error?
是什么导致了这个错误?
采纳答案by uncaught_exceptions
Try putting the driver jar in the server lib folder. ($CATALINA_HOME/lib)
尝试将驱动程序 jar 放在服务器 lib 文件夹中。($CATALINA_HOME/lib)
I believe that the connection pool needs to be set up even before the application is instantiated. (At least that's how it works in Jboss)
我相信甚至在应用程序实例化之前就需要设置连接池。(至少在 Jboss 中是这样工作的)
回答by Eric Leschinski
The reason you got this error:
您收到此错误的原因:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/dbname
Is because you forgot to register your mysql jdbc driver with the java application.
是因为你忘记用java应用程序注册你的mysql jdbc驱动程序。
This is what you wrote:
这是你写的:
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
} catch (SQLException e) {
throw new RuntimeException(e);
}
Should be this:
应该是这样的:
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);
}
You'll have to read the manual on your specific mysql jdbc driver to find the exact string to place inside the the Class.forName("...") parameter.
您必须阅读特定 mysql jdbc 驱动程序的手册,才能找到放置在 Class.forName("...") 参数中的确切字符串。
Class.forName not required with JDBC v.4
JDBC v.4 不需要 Class.forName
Starting with Java 6, Class.forName("something.jdbc.driver.YourFubarDriver")
is not necessary anymore if you use a recent (JDBC v.4) driver. For details read this: http://onjava.com/pub/a/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html
从 Java 6 开始,Class.forName("something.jdbc.driver.YourFubarDriver")
如果您使用最新的 (JDBC v.4) 驱动程序,则不再需要。有关详细信息,请阅读:http: //onjava.com/pub/a/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html
回答by ybenjira
I had the same problem using Tomcat7 with mysql-connector-java-5.1.26 that I put in both my $CATALINA_HOME/lib and WEB-INF/lib, just in case. But it wouldn't find it until I used either one of these two statements before getting the connection:
我在使用 Tomcat7 和 mysql-connector-java-5.1.26 时遇到了同样的问题,我把它放在了 $CATALINA_HOME/lib 和 WEB-INF/lib 中,以防万一。但是直到我在获得连接之前使用了这两个语句之一之前它才会找到它:
DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
OR
或者
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver");
I then followed up with removing mysql-connector-java-5.1.26 from $CATALINA_HOME/lib and the connection still works.
然后我从 $CATALINA_HOME/lib 中删除了 mysql-connector-java-5.1.26 并且连接仍然有效。
回答by bjethwan
When running tomcat out of eclipse it won't pick the lib set in CATALINA_HOME/lib
, there are two ways to fix it. Double click on Tomcat server in eclipse servers view, it will open the tomcat plugin config, then either:
当在 Eclipse 之外运行 tomcat 时,它不会选择 中的 lib 集CATALINA_HOME/lib
,有两种方法可以修复它。在 eclipse 服务器视图中双击 Tomcat 服务器,它将打开 tomcat 插件配置,然后:
- Click on "Open Launch Config" > Classpath tab set the mysql connector/j jar location. or
- Server Location > select option which says "Use Tomcat installation (take control of Tomcat installation)"
- 单击“打开启动配置”>“类路径”选项卡设置 mysql 连接器/j jar 位置。或者
- 服务器位置 > 选择“使用 Tomcat 安装(控制 Tomcat 安装)”选项
回答by nondescript
I had the mysql jdbc library in both $CATALINA_HOME/lib and WEB-INF/lib, still i got this error . I needed Class.forName("com.mysql.jdbc.Driver"); to make it work.
我在 $CATALINA_HOME/lib 和 WEB-INF/lib 中都有 mysql jdbc 库,但仍然出现此错误。我需要 Class.forName("com.mysql.jdbc.Driver"); 使其工作。
回答by test30
I had the same problem, all you need to do is define classpath environment variable for tomcat, you can do it by adding a file, in my case C:\apache-tomcat-7.0.30\bin\setenv.bat
, containing:
我有同样的问题,你需要做的就是为 tomcat 定义类路径环境变量,你可以通过添加一个文件来完成,在我的例子中C:\apache-tomcat-7.0.30\bin\setenv.bat
,包含:
set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\lib\mysql-connector-java-5.1.14-bin.jar"
then code, in my case:
然后编码,在我的情况下:
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "root", "");
works fine.
工作正常。
回答by Tunde Pizzle
add the artifact from maven.
从 Maven 添加工件。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
回答by Joan Payano Camacho
Use:
用:
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Registro exitoso");
} catch (Exception e) {
System.out.println(e.toString());
}
DriverManager.getConnection(..
回答by Maulik
Bro, you can also write code as below:
兄弟,你也可以写如下代码:
import java.sql.*;
import java.io.*;
public class InsertDatabase {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/Maulik","root","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from Employee");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
回答by nick
I'm running Tomcat 7 in Eclipse with Java 7 and using the jdbc driver for MSSQL sqljdbc4.jar.
我在 Eclipse 中使用 Java 7 运行 Tomcat 7,并使用 MSSQL sqljdbc4.jar 的 jdbc 驱动程序。
When running the code outside of tomcat, from a standalone java app, this worked just fine:
在 tomcat 之外运行代码时,从一个独立的 java 应用程序,这工作得很好:
connection = DriverManager.getConnection(conString, user, pw);
However, when I tried to run the same code inside of Tomcat 7, I found that I could only get it work by first registering the driver, changing the above to this:
但是,当我尝试在 Tomcat 7 中运行相同的代码时,我发现我只能通过首先注册驱动程序才能使其工作,将上面的更改为:
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
connection = DriverManager.getConnection(conString, user, pw);