0

I am trying to connect to a database using MySQL. The error I get is:
"com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure"

Any ideas what can cause this?

import java.sql.*;
import java.sql.DriverManager;

public class Conn {

    Connection c=null;
   // Statement s;
    public Conn(){
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            c=DriverManager.getConnection("jdbc:mysql://localhost::3306/bankingsystem","root","password");
          //  Statement statement=c.createStatement();
         //   ResultSet rs=statement.executeQuery();
            if(c!=null)
            {
                System.out.println("Connected to DB");
            }
        }catch (Exception e){
            System.out.println("Not connected to DB");
            System.out.println(e);

        }
    }

    public static void main(String[] args) {
        //Conn Conexiune =new Conn();
        

    }
}
  • Possibly related: [com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure](https://stackoverflow.com/q/2983248) – Pshemo Mar 28 '23 at 15:56
  • 1
    BTW instead of `System.out.println(e);` consider using `e.printStackTrace()` to get possibly more info about cause of the exception (with line numbers). – Pshemo Mar 28 '23 at 16:00
  • 1
    Remove one of the colons between host and port in your URL: `jdbc:mysql://localhost: **:** 3306/bankingsystem` -> `jdbc:mysql://localhost:3306/bankingsystem` Should solve your problem! – The Frozen One Mar 28 '23 at 16:14
  • Please post the full exception stacktrace. – Mark Rotteveel Mar 28 '23 at 16:32

1 Answers1

0

You'll need to look further down the exception because the original exception is chained together with this one. This is the top of the exception:

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

So this failure to communicate isn't detailed enough to diagnose the issue. But if you find a java.net.ConnectException: Connection refused embedded it's likely that MySQL isn't running.

Change your System.out.println(e) -> e.printStackTrace() to see the full exception chain.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138