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

标签 java jdbc

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

收到错误消息“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/

相关文章:

kotlin - 使用 Kotlin Exposed 创建数据库后连接到数据库

org.h2.Driver 的 java ClassNotFoundException

java - 标准 XAConnectionHandle :prepareStatement should not be used outside an EJBServer

java - Activity 的 AlertDialog 样式按钮

java - 尝试使用 netbeans 输入数据时出现 SQL 错误

java - 加载 native 库的单元测试 Java 类

java - 与远程 Mercurial 和 Git http 存储库交互

java - JPA:应用程序必须提供 JDBC 连接

java - 扩展 "Dynamic Web Project"向导

java - 如何通过jdbc连接mysql