Java一直说找不到jdbc mysql驱动程序 [英] Java keeps saying it can't find the jdbc mysql driver

查看:422
本文介绍了Java一直说找不到jdbc mysql驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于jdbc的mysql驱动程序的另一个问题.考虑到我在Google上搜索时获得的搜索结果的数量,我发现在搜索结果中没有找到适合我的东西.

Here's yet another question about jdbc's mysql driver. Considering the number of search results I got when I googled, I'm pretty bummed nothing I found in them worked for me.

错误:

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();
        }
    }
}

我确定90%的/usr/share/java/mysql-connector-java.jar是该类的正确路径.这就是我在网上以及使用定位找到的东西.

I'm 90% certain /usr/share/java/mysql-connector-java.jar is the correct path for the class. That's what I've found both online, and using locate.

我尝试在/etc/environment中将环境类路径设置为CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar.如您所见,我也尝试过-cp标志.

I've tried setting the environment classpath to CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar in /etc/environment. As you can see, I've tried the -cp flag as well.

我可以使用命令行mysql-client使用JDBCTest类中的凭据连接到mysql服务器和数据库.因此,数据库服务器或我的用户名/密码不是错误.

I can connect to the mysql server and database with the credentials I have in the JDBCTest class using the command line mysql-client. So it is not an error with the db server or my user/password.

据我所知,我的jdbc网址是正确的.那是我搜索时发现的最常见的问题之一...

As far as I can tell, my jdbc url is correct. That was one of the more common problems I found when searching...

我正在服务器上使用Ubuntu 12.04 64bit.

I'm using Ubuntu 12.04 64bit on my servers.

libmysql-java已安装.照原样,openjdk-7-jre-headless.

libmysql-java is installed. As is, openjdk-7-jre-headless.

我完全在Tomcat外部运行此命令,因此所有将驱动程序复制到Tomcat目录中的答案都不适用.

I'm running this completely outside of Tomcat, so all the answers saying to copy the driver into Tomcat's directory shouldn't apply.

所以,我很困惑.我认为使用-cp标志将强制其工作.我的Java安装中是否缺少某些内容? openjdk-7-jre-headless遗漏了什么?

So, I'm stumped. I would think using the -cp flag would just force it to work. Is there something in my java install missing? Something that got left out of openjdk-7-jre-headless?

我该如何解决?

注意:此类仅是一项快速测试,可以帮助我诊断为什么较大的(专有)应用程序无法连接到我的数据库.较大的应用程序将引发相同的错误.我希望修复此小类将修复较大的应用程序.

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.

推荐答案

您可能正在使用不兼容JDBC 4的MySQL JDBC驱动程序版本,因此DriverManager不会自动加载该版本.在这种情况下,您需要使用以下命令显式加载它:

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. In that case you need to explicitly load it using:

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

另一种选择是使用兼容JDBC 4并自动加载的库版本.

The other option is to use a version of the library that is JDBC 4 compliant and will be automatically loaded.

这篇关于Java一直说找不到jdbc mysql驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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