unknown doubts
unknown doubts

Reputation: 13

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgres

import java.sql.*;
        
public class JDBCExample {
    static final String DB_URL = "jdbc:postgresql://localhost:5432/postgres";
    static final String USER = "postgres";
    static final String PASS = "Vinr";
    static final String QUERY = "SELECT id, first, last, age FROM Employees";
        
    public static void main(String[] args) {
        // Open a connection
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(QUERY);) {
            // Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                System.out.print("ID: " + rs.getInt("id"));
                System.out.print(", Age: " + rs.getInt("age"));
                System.out.print(", First: " + rs.getString("first"));
                System.out.println(", Last: " + rs.getString("last"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } 
    }
}

getting error as

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgres
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:708)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230)
        at JDBCExample.main(JDBCExample.java:11)

Upvotes: 1

Views: 3428

Answers (1)

Jens
Jens

Reputation: 69505

You miss the postgresql driver in your classpath at runtime.

Download it and add it to your classpath.

If you use maven for building, add

<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.5.0</version>
</dependency>

to your pom

Upvotes: 2

Related Questions