2015-02-06 87 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