11

I can use jdbc:postgresql://host:port/database to connect to a database in postgresql server using jdbc driver.

But I wanted to connect to the postgresql server and find the database list there. when I used jdbc:postgresql://localhost:5432, I got an exception called

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

is there any other driver or any method to get a connection to the server without knowing the database names in server and query the database list there?

5

3 Answers 3

17

Ok. I have figured it out my self. I can use this string to connect to the server with jdbc driver.

jdbc:postgresql://localhost:5432/?

and can use this code snippet to get the database list

private void listDownAllDatabases() {
        try {
            PreparedStatement ps = connection
                    .prepareStatement("SELECT datname FROM pg_database WHERE datistemplate = false;");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
            rs.close();
            ps.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

References: I used this dba stackexchange answer to get all the database list

Sign up to request clarification or add additional context in comments.

1 Comment

One important detail, is that user and password must not be specified. Otherwise it can't connect.
0

AFAIK, that is beyond the capabilities of JDBC. I suggest you reconsider your approach to the bigger problem. Environment variables or a PROPERTIES file might be a solution.

4 Comments

can you please more elaborate what this mean Environment variables or a PROPERTIES file might be a solution.?
Often, the reason the application doesn't know the database is because the application runs in different environment, such as Dev, Test, UAT, Prod. This generally involves a different server as well as the database, but the idea is the same. The idea is to describe the database (or server) external to the application so the value can be set, generally by administrators.
It's not beyond the capabilities of jdbc.please read my answer.I have tested it and it works properly.
That's interesting. Obviously a capability built into PostgreSQL. Looks like MySql supports something similar.
0
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Dev Parzival
 */
public class TestingDatabase {
    static String driver="org.postgresql.Driver";
    static String jdbc_url="jdbc:postgresql://localhost:5432/";
    static String username="username",password="password";
    static PrintStream out;
    static{
        out=System.out;
    }
    public static void main(String $[]){
        //SELECT datname FROM pg_database
        try{
            Class.forName(driver);
            Connection connection= DriverManager.getConnection(jdbc_url,username,password);
            Statement statement=connection.createStatement();
            ResultSet result=statement.executeQuery("SELECT datname FROM pg_database");
            while(result.next()){
                System.out.println(result.getString(1));
            }
            connection.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

Hope this might help someone somehow

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.