0

I have installed Oracle Database 21c Express. I want to connect to it using JDBC. However I'm getting following error:

No suitable driver found for jdbc:oracle:thin:@localhost:1521:XE

I am using JDK 17 and ojdbc11.jar. Is this correct ojdbc version for JDK17?

I have set CLASSPATH in Environment Variables ⟶ System variables in CLASSPATH variable.

enter image description here

Following is the code that I am trying:

public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "test");
        System.out.println("connection Establisted");
        System.out.println(connection);
        connection.close();
        connection = null;
    }

Following is the error that I am getting:

enter image description here

I have read that the following line is not required, is that true?

Class.forName("oracle.jdbc.driver.OracleDriver");

Also as I have added the ojdbc jar file in the CLASSPATH, I have not added it anywhere in the project files. Is that correct?

Jay.
  • 191
  • 12
  • Yes, you need Class.forName() to ensure the driver class is loaded into memory. I recommend reading this: https://www.tutorialspoint.com/jdbc/jdbc-db-connections.htm – Alex Sep 05 '22 at 21:09
  • @Alex I have followed the tutorial link, but I get the same error. Using Class.forName, I get ClassNotFoundException. Can you tell if I have added the ojdbc.jar in the windows CLASSPATH, do I need to copy that jar file somewhere in the structure too? – Jay. Sep 05 '22 at 21:22
  • 2
    How are you running the program? Is the classpath overridden on the command line? – Tim Moore Sep 05 '22 at 21:50
  • @TimMoore I'm using intelliJ IDEA and running the program using GUI (by clicking run button). Could you please explain what you meant. – Jay. Sep 05 '22 at 22:00
  • 3
    You [do **not** typically need `Class.forName()`](https://stackoverflow.com/a/28220844/12567365) with modern JDBC drivers (except in some relatively uncommon situations). I think the TutorialsPoint guide mentioned in another comment is fairly old. That same tutorial also mentions using `DriverManager.registerDriver()` - which is bad advice for modern drivers (which use service loading via SPI). – andrewJames Sep 05 '22 at 22:22
  • 1
    Two notes: (a) As a test, try this: [Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project](https://stackoverflow.com/q/1051640/12567365). (b) For the various valid combinations of Java, Oracle DB and JDBC drivers, see [here](https://www.oracle.com/database/technologies/faq-jdbc.html) - specifically, the section _What are the Oracle JDBC releases Vs JDK versions?_. – andrewJames Sep 05 '22 at 22:29
  • 1
    @Jay IntelliJ IDEA manages the classpath it’s own way. Setting the `CLASSPATH` environment variable will not work when you’re running this way. Check the link in the comment from andrewJames. – Tim Moore Sep 05 '22 at 22:43
  • @andrewJames Even stronger, using `DriverManager.registerDriver` from user code has always been bad advice, as that method should only be called by JDBC drivers from the static initializer in their driver class(es). – Mark Rotteveel Sep 07 '22 at 13:44
  • The CLASSPATH is not used for most ways of executing Java applications, including when running from IntelliJ. You should not use it. Specify the classpath explicitly with `-cp`, or in the `Class-Path` entry in the manifest (for `-jar` execution). – Mark Rotteveel Sep 07 '22 at 13:47

0 Answers0