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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rex Yeigh
  • 7
  • 2

1 Answers1

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);
gijoe
  • 1,159
  • 7
  • 7