gpt4 book ai didi

java - 在 Java 中连接到 Oracle DB 返回 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 19:09:20 25 4
gpt4 key购买 nike

我有这个方法来连接到oracle 11g xe。它仍然返回异常 java.lang.NullPointerException。我使用 Eclipse IDE。请帮忙,我不知道如何解决它。

public static Connection connectDB() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {

Connection connection = null;
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName).newInstance();

// Create a connection to the database
String serverName = "127.0.0.1";
String portNumber = "1521";
String sid = "xe";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "peter";
String password = "pass";
connection = DriverManager.getConnection(url, username, password);
System.out.println(connection);
} catch (ClassNotFoundException e) {
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
}
System.out.println(connection);
return connection;
}

最佳答案

问题出在 Oracle 驱动程序中。我通过将我需要的 ojdbc14.jar 文件放入服务器文件夹来解决这个问题。我使用的是 tomcat,所以我将 ojdbc14.jar 文件放入文件夹 apache-tomcat-7.0.8\lib\

关于java - 在 Java 中连接到 Oracle DB 返回 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810831/

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