无法使用DriverManager连接到mysql数据库

时间:2013-02-06 05:37:35

标签: java mysql jdbc

好的我正在尝试使用以下代码连接到mysql数据库。我试着查看文档,但我只是没有运气。

以下是代码:

public class TESTSQL {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://SLibraryDev.db.996****.hostedresource.com";
static final String USER = "SLD";
static final String PASS = "F11!";

public static void main(String[] args) {

    Connection conn = null;
    Statement stmt = null;

    try {
        // STEP 2: Register JDBC driver
        Class.forName(JDBC_DRIVER);

        // STEP 3: Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        ...
        }

这是我得到的错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)

当我注释掉“Class.forName(JDBC_DRIVER)”并运行它时,我收到此错误:

java.sql.SQLException: No suitable driver found for jdbc:mysql://SLibraryDev.db.996****.hostedresource.com
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)

我认为我需要做的是下载并运行这个:http://dev.mysql.com/downloads/connector/j/我做了但是看起来这个安装程序出了问题...好像它从未完成,就像它切入了过程的中间。任何人都对我需要做的事情有任何想法。

3 个答案:

答案 0 :(得分:1)

从以下网址下载mysql-connector http://nearbuy.googlecode.com/files/mysql-connector-java-5.1.13-bin.jar

并将其添加到您的类路径中。

如果您使用的是netbeans,要将此文件添加到您的项目中,请执行以下操作

右键单击您的项目>点击库>点击添加JAR /文件夹>然后选择下载的jar文件

答案 1 :(得分:0)

您需要下载mysql jdbc驱动程序jar并将其包含在类路径中

答案 2 :(得分:0)

java.sql.SQLException: No suitable driver found for jdbc:mysql://SLibraryDev.db.996****.hostedresource.com
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)

清楚地表明你的班级路径中没有司机

只需将mysql驱动程序jar文件添加到类路径

相关问题