我想創建一個使用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");
}
}
}
在classpath中是否有連接器/ j JAR? – 2015-02-06 17:30:41
[連接到JDBC MySQL數據庫]的可能的重複(http://stackoverflow.com/questions/28286467/connection-to-a-jdbc-mysql-databse) – Jens 2015-02-06 17:34:11
不應該你的dbUrl是這樣的:「jdbc: MySQL的//,某:3306/someDbName「? – alterfox 2015-02-06 17:41:47