[英]Java Connecting to a MySQL Database using 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");
}
}
}
您必須將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");
}
}
}
請將mysql JDBC Driver添加到您的類路徑,然后重試。 您可以下載它從這里或者在這里 ,如果你沒有它,並加載它。 它應該可以正常工作。 這是一個示例代碼:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.