我在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)
答案 0 :(得分:0)
尝试使用最新的JDBC驱动程序,您可以从此处下载:https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html。该错误应消失。