天天看点

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.