Fresh install of JDBC for Oracle - does not execut

2019-08-02 11:42发布

问题:

This is my first attempt to connect to an Oracle DB (10g) using JDBC. I have JDK 6 installed on my machine and trying to connect to an Oracle DB on a server. I have downloaded ojdbc6.jar and copied it to C:\oracle\product\10.2.0\client_1\jdbc\lib.

I have also set the following CLASS PATH:

.;
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

The following code gets compiled, however when I try to run the class file, it throws an error

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)

I save my .java files in C:\ to compile and run from this directory.

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...I added the .jar file directly in the CLASSPATH and it compiled but when I run the class file I get the following error message

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)

回答1:

Executing with the following command should get it to run.

java -jar -classpath .;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; jarName.jar

But, you might want to look into packaging these jars with your executable rather than setting the classpath.



回答2:

I am sure that something is wrong with your classpath.

Try adding the jar with driver to your project directly and see, whether it works. Then I would check for typos in your CP.



标签: java oracle jdbc