-1
import java.sql.*;
public class a2 {
  public static void main(String[] args) {
    try{
      Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/univ_db", "postgre", "astronaut.pd");
      PreparedStatement pstmt = con.prepareStatement("select id from student");
      ResultSet Rs = pstmt.executeQuery();
      while(Rs.next()){
        System.out.println(Rs.getInt(5));
      }
    }catch(Exception ex){
      System.out.println(ex.getMessage());
    }}}

I am trying to connect my server with my databse, and I do not know what i did wrong on this. I tried to search it online but I did not find any solution.

Error:

No suitable driver found for jdbc:postgresql://localhost:5432/univ_db

Larnu
  • 61,056
  • 10
  • 27
  • 50
  • What does this have to do with SQL Server? This appears to be related to Java and PostgreSQL (and nothing to do with SQL as a language, or the RDBMS SQL Server). I've updated the tags for what this appears to actually be related to. – Larnu Oct 06 '19 at 22:45
  • Also, that comment should be in your question, not as a comment. I've edited it in for you, but please do make use of the edit feature. Thanks. – Larnu Oct 06 '19 at 22:47
  • As I don't see an error in the URL itself, this means that likely you do not have the PostgreSQL JDBC driver on the class path when running your application. – Mark Rotteveel Oct 07 '19 at 09:24

1 Answers1

0

To solve this error you need to get driver class reference using Class.forName() before getting connection using DriverManager.getConnection()

Example:

Class.forName(“org.postgresql.Driver”);

Important: Remember to import postgree driver to your java project.

Guilherme Martin
  • 801
  • 1
  • 7
  • 16
  • This is unnecessary in a simple Java application since 2006 with Java 6 (assuming a JDBC 4 or higher compliant driver) as the driver is loaded automatically. More likely the OP doesn't have the driver on the class path. – Mark Rotteveel Oct 07 '19 at 09:23