1

The code below is exactly the same from a YouTube video, but it does not work for me. The expected output is to printout "connected" to the system, which means that JDBC is successfully connected to the database. But I get this error:

com.mysql.jdbc.exceptions.jdbc4.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 my code:

package sample;

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

public class Controller {
    private static final String username = "root";
    private static final String password = "";
    private static final String connection = "jdbc:mysql://localhost/hello";

    public static void main(String[] args) {
        Connection conn = null;

        try {
            conn = DriverManager.getConnection(connection, username, password);
            System.out.println("Connected");
        }
        catch (SQLException se) {
            System.err.println(se);
        }
    }
}

I've added the JDBC successfully as a library, exactly like the video showed but it doesn't work. I saw similar questions on Stack Overflow but none of them solved my problem. Any help will be appreciated, thanks!

JDBC version: mysql-connector-java-5.1.47-bin.jar

The database table

Bobulous
  • 12,967
  • 4
  • 37
  • 68
Abdullah Ajmal
  • 367
  • 3
  • 14
  • 1
    That error means it cannot connect. It's not an issue with your Java code, but rather your connection string. You may want to try explicitly specifying the port number and confirm hostname, etc. – Chris Thompson Apr 07 '19 at 13:51
  • I tried almost everything I could find on the internet, but it didn't work, btw I am using a Mac but the video was done on windows, so do you think it's the same or it's different on Mac? – Abdullah Ajmal Apr 07 '19 at 13:59
  • It shouldn't be and hasn't been in my experience. You could try changing the connection string to `"jdbc:mysql://localhost:3306/hello"`. I'm assuming the screen shot is from something running on the same computer? – Chris Thompson Apr 07 '19 at 14:03
  • Try to replace localhost in your connection string with your (internal) IP. – Joachim Rohde Apr 07 '19 at 14:04
  • Thanks for the answers, but no luck still :( – Abdullah Ajmal Apr 07 '19 at 14:16
  • 1
    I doubt MySql is running on your machine or the credentials are correct. An empty pw on a Mac, maybe. I m not a Mac user. – PeterMmm Apr 07 '19 at 14:21
  • Yes the password is empty. Most of the videos I've watched all show that the password is empty – Abdullah Ajmal Apr 07 '19 at 14:23
  • Have you verified that a MySQL server is actually running on your computer? – VGR Apr 07 '19 at 15:10
  • Yes, the screenshot shows my database – Abdullah Ajmal Apr 07 '19 at 15:31
  • Have you tried setting a root password and connecting with a password? It might be that your MySQL server doesn't allow connections without a password. – entreprenr Apr 07 '19 at 15:41
  • @Abdullah - Do you connect to phpMyAdmin using a URL similar to `http://localhost/...` ? – Gord Thompson Apr 07 '19 at 15:43
  • Yes, this is the URL: http://localhost:8080/phpmyadmin/sql.php?server=1&db=hello&table=userNames&pos=0 – Abdullah Ajmal Apr 07 '19 at 15:49
  • @entreprenr I just tried that out, it doesn't work. This problem is only on my Mac, I just tried my friend's code, which works on his pc, it does not work for me. Btw I have downloaded a platform-independent driver: mysql-connector-java-5.1.47-bin.jar – Abdullah Ajmal Apr 07 '19 at 15:54
  • 1
    @AbdullahAjmal - In phpMyAdmin, do `SHOW VARIABLES LIKE 'port'` and see what it returns. – Gord Thompson Apr 07 '19 at 17:22
  • Ok I did that and the variable_name is port and the value is 3306 – Abdullah Ajmal Apr 07 '19 at 18:01

1 Answers1

1

Link failure means DB is not reachable, Kindly confirm your DB host, port, username and password. If everything is correct, kindly confirm DB server is running and has yet to hit max connection limit.

and you are using JDBC jar file, this may be corrupted. so check it and I suggest you, import JDBC using Maven

and you did not register JDBC driver. first, register it

by using Class.forName("com.mysql.jdbc.Driver"); this

here is one example for your help,

// JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/EMP";

   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT id, first, last, age FROM Employees";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
      }

I assumed you put the appropriate JDBC dependency in your maven file and use com.mysql.cj.jdbc.Driver as class for newer versions of MySQL-connector

The name of the class that implements java.sql.Driver in MySQL Connector/J has changed from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver. The old class name has been deprecated.

majurageerthan
  • 2,169
  • 3
  • 17
  • 30