I'm learning how to connect Java to PostgreSQL, but doing almost everything manually so I learn the basics.
Here is my code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
class Test {
public static void main(String[] args) {
Connection myConnection;
PreparedStatement stmt;
try {
myConnection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "admin", "admin");
stmt = myConnection.prepareStatement("SELECT * FROM table");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
rs.getString("bird");
}
} catch (SQLException sqlException) {
System.out.println(sqlException.getMessage());
}
}
}
My Java version is 20, and I've put postgresql-42.6.0.jar
in C:\libs\
.
I compiled my Test.java
file with javac -cp C:\libs\postgresql-42.6.0.jar Test.java
, and also tried with path between double quotes, but when I try to run Test.class
with java Test
I get the following error:
No suitable driver found for jdbc:postgresql://localhost:5432/testdb