gpt4 book ai didi

jdbc - 无法在简单的 java 类中加载 OracleDriver 类

转载 作者:行者123 更新时间:2023-12-01 08:14:16 26 4
gpt4 key购买 nike

//DbConnector.java

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

public class DbConnector {

public static void main(String[] argv) {

System.out.println("-------- Oracle JDBC Connection Testing ------");

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

} catch (ClassNotFoundException e) {

System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;

}

System.out.println("Oracle JDBC Driver Registered!");

Connection connection = null;

try {

connection = DriverManager.getConnection(
"jdbc:oracle:thin:@172.17.119.108:1526:GESSDEV", "username",
"password");

} catch (SQLException e) {

System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;

}

if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}

}

我有一个名为 odbc14.jar 的 jdbc jar

javac -classpath odbc14.jar; DbConnector.java

编译成功

java -classpath odbc14.jar;数据库连接器

输出::-------- Oracle JDBC 连接测试 ------您的 Oracle JDBC 驱动程序在哪里?

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at DbConnector.main(DbConnector.java:13)

请帮我解决这个问题

我已经解决了上面的问题。现在我遇到了新的异常

-------- Oracle JDBC 连接测试 ------Oracle JDBC 驱动程序已注册!连接失败!检查输出控制台

java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:420)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(DriverManager.java:525)

最佳答案

ojdbc14.jar 一起使用的 JDBC 驱动程序是 oracle.jdbc.OracleDriver

参见:Loading JDBC Driver Class - ojdbc14.jar

关于jdbc - 无法在简单的 java 类中加载 OracleDriver 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14770791/

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