0

I have used below java code to connect to oracle database

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","root");

My java version is "18.0.1.1". enter image description here

My oracle version is 11.2g. I added the ojdbc6.jar and ojdbc6_g.jar to the path C:\Program Files\Java\jdk-18.0.1.1\lib.

I have added java bin and lib folder path to system variables under path enter image description here

But still when I try to run the program, I am getting the below error: ClassNotFoundException: oracle.jdbc.driver.OracleDriver.

Can anyone please help me to solve the problem. I tried all possible ways from stackoverflow and other resources but couldn't solve the issue.

  • 1
    Those JARs do not go in the JDK /lib folder. You should never, ever add anything to that directory. The right thing to do is to learn how to add 3rd party JARs to the CLASSPATH at compile and runtime. The LTS versions of JDK are 11 and 17. Any particular reason why you've chosen to use JDK 18? – duffymo Jun 26 '22 at 20:55
  • **Remove** your `Class.forName` code. [It hasn’t been needed for a very long time.](https://docs.oracle.com/en/java/javase/18/docs/api/java.sql/java/sql/package-summary.html) – VGR Jun 26 '22 at 23:47
  • @duffymo No any particular reason. I downloaded latest one which I got from oracle site. Can you please tell me how to add 3rd party jars from cmd? – Shrutika Subhash Dorugade Jun 27 '22 at 06:39
  • @VGR I have removed that and now getting this error: Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:xe – Shrutika Subhash Dorugade Jun 27 '22 at 06:40
  • ojdbc6 is for Java 6. Use a newer driver. – Mark Rotteveel Jun 27 '22 at 08:01
  • @VGR Although `Class.forName` is not necessary when the driver is on the initial classpath, it is still helpful as a debugging tool (i.e. does the class get loaded), and when the driver is not on the initial classpath, but on a context classpath (e.g. it's in the WEB-INF/lib of a WAR), loading with `Class.forName` can still be necessary. – Mark Rotteveel Jun 27 '22 at 08:04

1 Answers1

-1

Finally, I got the solution for this error. So, first make sure that you have added oracle bin path and jdk bin path under path variable. Then under system variables create new variable CLASSPATH and add oracle jar path and jdk bin path to that variable.

Variable Name: CLASSPATH
Variable Value: D:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6.jar;C:\Program Files\Java\jdk-18.0.1.1\bin;
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197