2015-02-06 93 views
0

我想創建一個使用JDBC連接到MySQL數據庫的程序。但是,當我嘗試運行它,我得到以下錯誤: -Java使用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"); 
     } 

    } 

} 
+0

在classpath中是否有連接器/ j JAR? – 2015-02-06 17:30:41

+1

[連接到JDBC MySQL數據庫]的可能的重複(http://stackoverflow.com/questions/28286467/connection-to-a-jdbc-mysql-databse) – Jens 2015-02-06 17:34:11

+1

不應該你的dbUrl是這樣的:「jdbc: MySQL的//,某:3306/someDbName「? – alterfox 2015-02-06 17:41:47

回答

1

你必須使用mysql-connector-java的5.1.18斌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

請將mysql JDBC Driver添加到您的類路徑中,然後重試。如果您沒有並加載它,可以從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; 
} 
+0

嗨。感謝帖子:-)。我已經下載了JDBC驅動程序並將其添加到我的項目中。但我仍然有同樣的問題。 – ED209 2015-02-07 14:35:11

+0

你有沒有檢查過你的dbURL因爲它應該以jdbc:mysql:ip:port/db語法 – Olu 2015-02-09 04:31:32

+0

開始,哪個ide是你用的。 – Olu 2015-02-09 04:32:19