How to resolve 'No suitable driver found' error

13,388

Solution 1

I quickly tried your code and first got the same error:

With the correction to: DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres","gautam"); it worked.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class NewClass {

    public void initialize() {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Class not found " + e);
        }
        try {
            Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres","gautam");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM role");
            System.out.println("id  name");

            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                System.out.println(id + "   " + name);

            }
        } catch (SQLException e) {
            System.out.println("SQL exception occured" + e);
        }
    }

    public static void main(String[] args) {
        new NewClass().initialize();
    }

}

The DriverManager asks every driver that is registered to it if it can read the url: "jdbc:postgresql://localhost:5432/postgres".
The first driver that returns true is used.
In your case no driver returned true.
The Method of the driver, that returns true or false is acceptsURL("jdbc:postgresql://localhost:5432/postgres")

You can test it with:

    try {
        Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver nextElement = drivers.nextElement();
            JOptionPane.showMessageDialog(null, nextElement.acceptsURL("jdbc:postgresql://localhost:5432/postgres"));
            JOptionPane.showMessageDialog(null, nextElement.acceptsURL("Jdbc:postgresql://localhost:5432/postgres"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

Solution 2

I am using NetBeans 11.1 I 've got the same error message as in the title of this thread. I was able to compile and run my Java code on the Linux command line.

What I have overlooked since two days was the following error message in NetBeans, resp. I didn't connect this error message with my problem:

skip non existing resourceDirectory /home/thatsme/NetBeansProjects/myproject/src/main/resources

  1. I created this directory manually on the command line
  2. I copied the file with the postgresql driver "postgres-42.2.8.jar" into this directory

And since then my code runs also fine in NetBeans. I know, this is a very special error, but maybe it will help another one too. Just want to give something back here.

EDIT: I had the same problem with the ORACLE file ojdbc8.jar and my above mentioned solution didn't help. But after I unpacked ojdbc8.jar it worked fine.

$ jar xf ojdbc8.jar

If I run my application outside Netbeans/Maven then it works also fine with the packed version in the CLASSPATH.

Share:
13,388
Gautam
Author by

Gautam

Updated on June 04, 2022

Comments

  • Gautam
    Gautam almost 2 years

    I am running Netbeans 8.0.2. I was learning about JDBC and wanted to connect it to a PostgreSQL database. I looked up for all possible answers but no answer made it work.

    I have also chosen the library on the left side menu as PostgreSQL JDBC Driver -postgresql-9.2-1002.jdbc4.jar

    The error shown is:

    SQL exception occuredjava.sql.SQLException: No suitable driver found for Jdbc:postgresql://localhost:5432/postgres

    Here's the code:

    try   { 
    
        Class.forName("org.postgresql.Driver");
    
      }
      catch(ClassNotFoundException e) {
         System.out.println("Class not found "+ e);
      }
         try {
    
         Connection con = DriverManager.getConnection
         ("Jdbc:postgresql://localhost:5432/postgres","postgres",
         "gautam");
    
         Statement stmt = con.createStatement();
         ResultSet rs = stmt.executeQuery
         ("SELECT * FROM role");
         System.out.println("id  name");
    
         while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            System.out.println(id+"   "+name);
    
         }
      }
      catch(SQLException e){
         System.out.println("SQL exception occured" + e);
      }