简体   繁体   English

Java一直说它无法找到jdbc mysql驱动程序

[英]Java keeps saying it can't find the jdbc mysql driver

Here's yet another question about jdbc's mysql driver. 这是关于jdbc的mysql驱动程序的另一个问题。 Considering the number of search results I got when I googled, I'm pretty bummed nothing I found in them worked for me. 考虑到我用Google搜索时获得的搜索结果的数量,我很沮丧,因为我发现它们对我起作用。

The error: 错误:

hostname# java -cp /usr/share/java/mysql-connector.jar:/home/user JDBCTest
java.sql.SQLException: No suitable driver found for jdbc:mysql://<db ip>:3306/dbname
 at java.sql.DriverManager.getConnection(DriverManager.java:596)
 at java.sql.DriverManager.getConnection(DriverManager.java:215)
 at JDBCTest.main(sqltest.java:14)

The code (pulled from a short how to): 代码(从简短的方法拉到):

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

class JDBCTest {

    private static final String url = "jdbc:mysql://dbipaddress:3306/dbname";

    private static final String user = "username";

    private static final String password = "password";

    public static void main(String args[]) {
        try {
            Connection con = DriverManager.getConnection(url, user, password);
            System.out.println("Success");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I'm 90% certain /usr/share/java/mysql-connector-java.jar is the correct path for the class. 我90%肯定/usr/share/java/mysql-connector-java.jar是该类的正确路径。 That's what I've found both online, and using locate. 这就是我在网上找到的,并使用了locate。

I've tried setting the environment classpath to CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar in /etc/environment. 我已经尝试将环境类路径设置为/ etc / environment中的CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar As you can see, I've tried the -cp flag as well. 如您所见,我也尝试过-cp标志。

I can connect to the mysql server and database with the credentials I have in the JDBCTest class using the command line mysql-client. 我可以使用命令行mysql-client使用JDBCTest类中的凭据连接到mysql服务器和数据库。 So it is not an error with the db server or my user/password. 所以这不是db服务器或我的用户/密码的错误。

As far as I can tell, my jdbc url is correct. 据我所知,我的jdbc网址是正确的。 That was one of the more common problems I found when searching... 这是我在搜索时遇到的更常见的问题之一......

I'm using Ubuntu 12.04 64bit on my servers. 我在我的服务器上使用Ubuntu 12.04 64bit。

libmysql-java is installed. 已安装libmysql-java。 As is, openjdk-7-jre-headless. 就像是,openjdk-7-jre-headless。

I'm running this completely outside of Tomcat, so all the answers saying to copy the driver into Tomcat's directory shouldn't apply. 我在Tomcat之外完全运行它,因此将驱动程序复制到Tomcat目录中的所有答案都不适用。

So, I'm stumped. 所以,我很难过。 I would think using the -cp flag would just force it to work. 我认为使用-cp标志只会强制它工作。 Is there something in my java install missing? 我的java安装中有什么东西丢失了吗? Something that got left out of openjdk-7-jre-headless? 什么东西从openjdk-7-jre无头?

How do I fix this? 我该如何解决?

Note: This class is just a quick test to help me diagnose why a larger (proprietary) app will not connect to my db. 注意:这个类只是一个快速测试,可以帮助我诊断为什么更大的(专有)应用程序无法连接到我的数据库。 The larger app throws the same error. 较大的应用程序会抛出相同的错误。 I'm hoping that fixing this small class will fix the larger app. 我希望修复这个小类将修复更大的应用程序。

You are probably using a version of the MySQL JDBC driver that is not JDBC 4 compliant, so it is not automatically loaded by DriverManager . 您可能正在使用不符合JDBC 4的MySQL JDBC驱动程序版本,因此DriverManager不会自动加载它。 In that case you need to explicitly load it using: 在这种情况下,您需要使用以下方法显式加载它:

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

The other option is to use a version of the library that is JDBC 4 compliant and will be automatically loaded. 另一种选择是使用符合JDBC 4的库版本,并自动加载。

Try adding the following on the first line of your main method: 尝试在main方法的第一行添加以下内容:

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

If it throws an exception, then the JVM cannot access /usr/share/java/mysql-connector.jar . 如果它抛出异常,则JVM无法访问/usr/share/java/mysql-connector.jar If that is the case, then check file permissions using: 如果是这种情况,请使用以下命令检查文件权限:

ls -lah /usr/share/java/mysql-connector.jar

You should have at least read access to this file, and obviously the file should exist. 您应该至少具有对此文件的读取权限,显然该文件应该存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM