0

I'm learning about Java JDBC and I want to connect with MySQL. I've added a connection driver, updated JDK and MySQL to the newest versions, set up MySQL online, but I still get an error about a Communications link failure. Here is my code:

package jdbcConnection;

import java.sql.*;

public class ConnectionTest {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost::3306/tree","root","pass");
            Statement stmt = con.createStatement();
            System.out.println("Inserting records");
            String sql = "Insert into fruit values(43)";
            stmt.executeUpdate(sql);
            con.close();
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
}

This is the error:

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.
khelwood
  • 55,782
  • 14
  • 81
  • 108
Comet
  • 13
  • 3
  • Maybe [this](https://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql) can help you. – GregGott May 02 '23 at 10:25

0 Answers0