1

I went to Database > + > Data Source > MySQL

Here's what my panel looks like: enter image description here

This is the error:

[08S01]
    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.
java.net.ConnectException: Connection refused: connect.

and here's my connection file:

package sample;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection
{

    private static Connection connection;
    private static final String user = "root";
    private static final String password = "root";
    private static final String database = "jdbc:mysql://localhost:3306/user";

    public static Connection getConnection()
    {
        if (connection == null)
        {
            try
            {
                connection = DriverManager.getConnection(database, user, password);
            } catch (SQLException e)
            {
                e.printStackTrace();
                System.out.println("Could not open database.");
                System.exit(1);

            }
        }
        return connection;
    }

}

What could be my problem? I tried searching up for what others have tried but none of them seemed to work and I had to make several copies of my entire project because I kept messing things up trying those solutions to no avail. More specifically Solving a "communications link failure" with JDBC and MySQL

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Johnny Robertson
  • 117
  • 1
  • 3
  • 13
  • 1
    Do you have the server running and listening on port 3306? Veirfy with the https://learn.microsoft.com/en-us/sysinternals/downloads/tcpview. Can you connect to this server using any other tools? Is there any firewall that can block the connection? – CrazyCoder Mar 11 '21 at 23:14
  • @CrazyCoder let's say it's not running, how would I make the server run on port 3306? – Johnny Robertson Mar 11 '21 at 23:28
  • Did you try to follow https://dev.mysql.com/doc/refman/8.0/en/windows-installation.html and https://dev.mysql.com/doc/refman/8.0/en/starting-server.html? – CrazyCoder Mar 11 '21 at 23:29
  • @CrazyCoder dang, I did not do either of those. I followed this tutorial and that's all I thought i had to do: https://www.youtube.com/watch?v=-yiL99KFeng&t=779s – Johnny Robertson Mar 11 '21 at 23:30
  • @CrazyCoder thanks, ill look in to it and give it a try – Johnny Robertson Mar 11 '21 at 23:30

2 Answers2

1

I suppose that your program did not run and so you went to configure some datasource in Intelij to make a connection with your Database.

If that is the case then

  1. Your db is not reachable. Check if it is running and if all information provided here is correctly.

  2. You don't need to set a InteliJ Datasource for your program to run. That is only for you to execute sql scripts using Intelij. It has nothing to do with some program that you develop using java which connects to that database.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
1

You need to have MySQL server installed and running to be able to connect to it from the other apps.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904