Java使用JDBC连接到MySQL数据库

时间:2015-02-06 17:28:08

标签: java mysql maven jdbc

我正在尝试创建一个使用JDBC连接到MySQL数据库的程序。但是当我尝试运行它时,我得到以下错误: -

java.sql.SQLException: No suitable driver found for a9442ca6-992c-411b-8bda-a42f00a0ab2e.mysql.sequelizer.com
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at DVDLibrary.MySQLDBConnection.testConnect(MySQLDBConnection.java:24)
    at DVDLibrary.MainClass.main(MainClass.java:12)

我已经安装了Maven并且已经将MySQL依赖项添加到POM文件中(参见下文)。但仍然无法让我的计划工作。请有人帮忙吗?

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.17</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

package DVDLibrary;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MySQLDBConnection {

        public void testConnect(){

        String dbUrl = "xxx";
        String username  = "xxx";
        String password  = "xxx";
        String dbClass = "com.mysql.jdbc.Driver";

        String query = "SELECT * FROM DVD Info Table";

        try {

            Class.forName(dbClass);
            Connection connection = DriverManager.getConnection(dbUrl, username, password);

            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);

            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                System.out.println(tableName);
            }
            connection.close();

        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

其他课程: -

package DVDLibrary;

import oracle.jrockit.jfr.tools.ConCatRepository;


public class MainClass {

     public static void main(String []args)
    {
    MySQLDBConnection con = new MySQLDBConnection();

    con.testConnect();

        if(con != null)
        {
            System.out.println("Succes"); 

        }
        else
        {
            System.out.println("Fail"); 
        }

    }

}

2 个答案:

答案 0 :(得分:1)

您必须将mysql-connector-java-5.1.18-bin jar添加到您的库中。

这是一个示例类,包含打开和关闭数据库连接的方法。

public class Database {
    public static Connection con() throws ClassNotFoundException, SQLException{
        Class.forName("com.mysql.jdbc.Driver");
        Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName", "User", "Password");

        return c ;
    }

    public static void con_close(Connection c) {
        try {
            if(c!=null)
            c.close();
        } catch (SQLException sQLException) {
            System.out.println(sQLException + "Database connection closing failure");
        }
    }

        public static void stmt_close(Statement s) {
        try {
            if(s!=null)
            s.close();
        } catch (SQLException sQLException) {
            System.out.println(sQLException + "Statement closing failure");
        }
    }
    public static void rs_close(ResultSet r) {
        try {
            if(r!=null)
            r.close();
        } catch (SQLException sQLException) {
            System.out.println(sQLException + "ResultSet closing failure");
        }
    }


}

答案 1 :(得分:1)

请将mysql JDBC驱动程序添加到类路径中,然后重试。如果您没有,可以从herehere下载并加载它。它应该工作得很好。 以下是示例代码:

 Connection getConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql:localhost:3306/Database", "username",
                "password");
        if (!conn.isClosed())
            return conn;
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
    return null;
}