0

I am unable to connect to a database on Pg Admin 4; despite being able to query the database using DB Browser in intellij. Below is the error I keep getting.

This same program could connect to the database however after trying to download a python sql tool something went wrong. I re-installed postgresql and created the tables as they were but I now can't connect to them.

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Here is the connectionclass.java which tries to access the database.

public class ConnectionClass {

    public Connection connection;
    public Connection getConnection() {
        String dbName = "users";
        String userName = "postgres";
        String password = "pass";

        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            connection = DriverManager.getConnection("jdbc:mysql://localhost:5432/" + dbName, userName, password);

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

Finally I have a screengrab of the sql query tool.

DB Browser

I've checked dozens of errors like this but they all seem to involve misspellings. I can change the database to an incorrect name, owner, or password but the error remains the same. I thought it might be the driver as I use mysql-connector-java-8.0.20.jar in the program but postgresql is what runs the database. I tried replacing the jar with postgresql-42.2.17.jar but that dosn't work either.

At this point I'm considering scrapping the whole thing and going to live on a mountain so any help is appreciated.

EDIT: Coping the jar into lib folder does not work until you use the method outlined here to unpack it. Might take a few tries but mine finally works. Thank you so much! If I had any reputation points I'd upvote.

1 Answer 1

1

Download the appropriate driver

https://jdbc.postgresql.org/download.html

load the "correct" driver ""org.postgresql.Driver" in your program as you do for mysql, do this instead

Class.forName("org.postgresql.Driver");

and connect with the correct protocol "jdbc:postgresql:"

Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + dbName, userName, password);
Sign up to request clarification or add additional context in comments.

3 Comments

Did this and copied the .jar into the lib folder but now I get the error: java.lang.ClassNotFoundException: org.postgresql.Driver
is this a console application or web application?
Console application.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.