gpt4 book ai didi

java - 收到错误“SQL 状态 : 08001 No suitable driver found for jdbc:oracle:thin:@128:23:44:01:12345:pppp_rr

转载 作者:行者123 更新时间:2023-12-02 02:43:09 41 4
gpt4 key购买 nike

有人可以建议我在这里做错了什么吗:

收到错误消息“SQL 状态:08001未找到适合 jdbc:oracle:thin:@128:23:44:01:12345:pppp_rr 的驱动程序已拾取 JAVA_TOOL_OPTIONS: -Duser.home=C:\Users\123ert"

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCExample {

public static void main(String[] args) {

try (Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@128:23:44:01:12345:pppp_rr", "Test123", "********")) {

if (conn != null) {
System.out.println("Connected to the database!");
} else {
System.out.println("Failed to make connection!");
}

} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}

}
}

enter image description here

最佳答案

我无法使用 Oracle 数据库实际测试它,但我知道在建立连接之前必须注册一个驱动程序。检查以下代码,这基本上是您的代码加上驱动程序注册。

public static void main(String args[]) throws Exception {
// registration for the driver, it's needed,
// otherwise there will be "no suitable driver found"
Class.forName("oracle.jdbc.driver.OracleDriver");

try (Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@128:23:44:01:12345:pppp_rr", "Test123", "********")) {

if (conn != null) {
System.out.println("Connected to the database!");
} else {
System.out.println("Failed to make connection!");
}

} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}

关于java - 收到错误“SQL 状态 : 08001 No suitable driver found for jdbc:oracle:thin:@128:23:44:01:12345:pppp_rr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57165433/

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