MySQL java.sql.SQLException:找不到适合本地主机测试的驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8509725/
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
java.sql.SQLException: No suitable driver found for localhost test
提问by user1035079
When trying to connect to mysql I always get this error:
尝试连接到 mysql 时,我总是收到此错误:
java.sql.SQLException: No suitable driver found for localhost test
java.sql.SQLException:找不到适合本地主机测试的驱动程序
I already included the mysql-connector.jar
in the /WEB-INF/lib
in my app. What else do I need to configure to make it work? Do I need to add something in web.xml
? I'm not using the appengine.
我已经包括mysql-connector.jar
在/WEB-INF/lib
我的应用程序。我还需要配置什么才能使其工作?我需要添加一些东西web.xml
吗?我没有使用 appengine。
Here is my code in the server:
这是我在服务器中的代码:
package com.mysql.server;
import java.sql.Connection;
import java.sql.DriverManager;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mysql.client.ConnDb;
public class ConnDbImpl extends RemoteServiceServlet implements ConnDb {
public Connection con;
@Override
public String tryConn() {
try{
String host = "localhost";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "pwd";
Class.forName(driver).newInstance();
con = DriverManager.getConnection(host+db, user, pass);
return "Connected to Database";
} catch(Exception ex) {
return ex.toString();
}
}
}
回答by BalusC
You will get this exception when the JDBC URL is not accepted by any of the loaded JDBC drivers as per the Driver#acceptsURL()
method. You actually forgot the JDBC driver specific URI prefix. For the MySQL JDBC driver this is jdbc:mysql://
. The full connection URL should look like this:
当 JDBC URL 不被任何加载的 JDBC 驱动程序按照该Driver#acceptsURL()
方法接受时,您将收到此异常。您实际上忘记了 JDBC 驱动程序特定的 URI 前缀。对于 MySQL JDBC 驱动程序,这是jdbc:mysql://
. 完整的连接 URL 应如下所示:
con = DriverManager.getConnection("jdbc:mysql://localhost/test", user, pass);
See also:
也可以看看:
回答by Richard
I found another cause for this error message. In my case the user simply had no privilege to the database e.g. to the selected table. Dear driver developers, why do you use such misleading error messages? A lot of people have real trouble with this.
我找到了此错误消息的另一个原因。在我的情况下,用户根本没有对数据库的权限,例如对所选表的权限。亲爱的驱动程序开发人员,您为什么使用这种具有误导性的错误消息?很多人在这方面遇到了真正的麻烦。
回答by S M Slack
For me, it was forgetting to include the MySQLJDBC Driver in the project libraries. DOH!
对我来说,忘记在项目库中包含 MySQLJDBC 驱动程序。哦!
回答by SkyTreasure
This was giving that error:
这是给那个错误:
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/lib_db","root","root");
but when I changed that to:
但是当我将其更改为:
Connection connection =DriverManager.getConnection("jdbc:mysql://localhost/db_name?"+"user=root&password=root");
Connection connection =DriverManager.getConnection("jdbc:mysql://localhost/db_name?"+"user=root&password=root");
error was gone
错误消失了