Reputation: 35
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);
}
Upvotes: 2
Views: 18597
Reputation: 11
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
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.
Upvotes: 1
Reputation: 4897
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();
}
Upvotes: 2