这是我第一次尝试使用 JDBC 连接到 Oracle DB (10g)。我的机器上安装了 JDK 6 并尝试连接到服务器上的 Oracle 数据库。我已经下载了 ojdbc6.jar并将其复制到C:\oracle\product\10.2.0\client_1\jdbc\lib。
我还设置了以下类路径:
.;
C:\;
C:\oracle\product\10.2.0\client_1\jdbc\lib\ojdbc6.jar;
C:\oracle\product\10.2.0\client_1\jlib\orai18n.jar;
C:\Program Files\QuickTime\QTSystem\QTJava.zip
以下代码已编译,但是当我尝试运行类文件时,它会引发错误
C:\>java JDBCVersion
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at JDBCVersion.main(JDBCVersion.java:10)
我将 .java 文件保存在 C:\ 中,以便从该目录编译和运行。
import java.io.*;
import java.sql.*;
class JDBCVersion
{
public static void main (String args[]) throws SQLException{
try{
Connection con=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(
"jdbc:oracle:thin:@HOSTNAME:1521:MYORADB",
"SCOTT",
"TIGER");
Statement s=con.createStatement();
s.execute("SELECT * FROM EMPLOYEE");
s.close();
con.close();
} catch(Exception e){e.printStackTrace();}
}
}
@dardo ...我直接在 CLASSPATH 中添加了 .jar 文件并对其进行了编译,但是当我运行类文件时,我收到以下错误消息
C:\>java JDBCVersion
Exception in thread "main" java.lang.NoSuchMethodError: oracle.i18n.text.convert
er.CharacterConverterOGS.getInstance(I)Loracle/i18n/text/converter/CharacterConverter;
at oracle.sql.converter.CharacterConverterFactoryOGS.make(CharacterConverterFactoryOGS.java:43)
at oracle.sql.CharacterSetWithConverter.getInstance(CharacterSetWithConverter.java:97)
at oracle.sql.CharacterSetFactoryThin.make(CharacterSetFactoryThin.java:129)
at oracle.sql.CharacterSet.make(CharacterSet.java:514)
at oracle.jdbc.driver.DBConversion.init(DBConversion.java:169)
at oracle.jdbc.driver.DBConversion.<init>(DBConversion.java:120)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1135)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at JDBCVersion.main(JDBCVersion.java:15)