天天看點

mysql jdbc安裝和使用_java – 如何安裝JDBC以及如何使用它連接配接到mysql?

我正在嘗試安裝JDBC,但我不知道如何,當你隻有jar檔案時,我将它複制到我的

java ext檔案夾,但它一直給我一個錯誤,誰能告訴我如何完成安裝驅動程式并使用它?

下面是我使用的代碼

import java.sql.*;

public class Test1

{

public static void main (String[] args)

{

String url = "jdbc:mysql://localhost:3306/sabayafr_sabmah";

String username = "root";

String password = "ma";

Connection connection = null;

try {

System.out.println("Connecting database...");

connection = DriverManager.getConnection(url, username, password);

System.out.println("Database connected!");

} catch (SQLException e) {

System.err.println("Cannot connect the database!");

e.printStackTrace();

} finally {

System.out.println("Closing the connection.");

if (connection != null) try { connection.close(); } catch (SQLException ignore) {}

}

}

}

以下是我得到的回應

Cannot connect to database server

更新#3

C:\Users\AlAsad\Desktop>java -cp .;mysql-connector-java-5.0.8-bin.jar Test1

Connecting database...

Cannot connect the database!

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

sabayafr_sabmah

at java.sql.DriverManager.getConnection(Unknown Source)

at java.sql.DriverManager.getConnection(Unknown Source)

at Test1.main(Test1.java:12)

Closing the connection.