gpt4 book ai didi

mysql - 没有找到适合 jdbc mysql 的驱动程序?

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:12 25 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个程序来连接到 eclipse 中的 MySQL 数据库,但我收到错误 “java.sql.SQLException:找不到合适的驱动程序”

java代码是:

import java.sql.*;

public class FirstExample {

//static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String S_DB_URL = "jdbc:mysql://localhost:3306/emp";
static final String S_USER = "root";
static final String S_PASS = "root";

public static void main(String[] args) {

try {

System.out.println("Connecting to database...");
//Class.forName(S_JDBC_DRIVER);
Connection connection = DriverManager.getConnection(S_DB_URL,
S_USER, S_PASS);

System.out.println("Creating statement...");
Statement statement = connection.createStatement();
String sql = "SELECT * FROM Employee";
ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {

int iId = resultSet.getInt("id");
int iAge = resultSet.getInt("age");
String sFirst = resultSet.getString("fname");
String sLast = resultSet.getString("lname");

System.out.print("ID: " + iId);
System.out.print("\tAge: " + iAge);
System.out.print("\tFirst: " + sFirst);
System.out.println("\tLast: " + sLast);
}

resultSet.close();
statement.close();
connection.close();
} catch (SQLException se) {

for (Throwable t : se) {
t.printStackTrace();
}
}
System.out.println("Goodbye!");
}

控制台选项卡中的输出是:

Connecting to database...
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/emp
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at FirstExample.main(FirstExample.java:21)
Goodbye!

我使用过 MySQL Connector/J。在MySQL安装目录下解压,将jar文件添加到CLASSPATH中。

另请参阅此图片。有一个 !在项目根目录标记。 image01

我收到如下图所示的错误:image02当我包含 2 个注释语句时:

static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
Class.forName(S_JDBC_DRIVER);

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com