我在 VM 的服务器和客户端上都有 CentOS。我正在尝试将 Java SE 8 与 Oracle 11gR2 连接起来。 但它给出了这个错误。我通过
ojdbc6.jar
和"project properties-> libraries -> Add Jar/folder->/usr/java/jdk1.8.0_20/jre/lib/ext/ojdbc6.jar
添加了ojdbc8.jar"
。
代码是:
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.*;
class OracleCon
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@serverora11gr2:1521:orcl","scott","tiger");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}
catch(Exception e)
{ System.out.println(e);}
}
}
错误是:
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class oracle.jdbc.driver.ClassRef
at oracle.jdbc.driver.OracleDriver.<clinit>(OracleDriver.java:262)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at OracleCon.main(OracleCon.java:10)
/home/rahul/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java
returned: 1
BUILD FAILED (total time: 0 seconds)
尝试最新的 JDBC 驱动程序,您可以从此处下载:https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html。这个错误应该消失了。
您可以尝试在 pom.xml 中重新排序(将 oracle 依赖项放在 sybase 之前/很多之前)sybase 和 ojdbc8 jar 依赖项
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<dependency>
<groupId>com.sybase.jconnect</groupId>
<artifactId>jconn4</artifactId>
<version>7.07-27082</version>
</dependency>