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.