title: Communications link failure when trying to connect to the database
body: <p>I want to insert data to my database using JDBC, so the first step is to connect to it:</p>
  
  <pre><code>try {
          Class.forName("com.mysql.jdbc.Driver");
      } catch (ClassNotFoundException e) {
          System.out.println("Where is your MySQL JDBC Driver?");
          e.printStackTrace();
          return;
      }
  
      System.out.println("MySQL JDBC Driver Registered!");
      Connection connection = null;
  
      try {
          connection = (Connection) DriverManager
          .getConnection("jdbc:mysql://localhost:3306/fruits","root", "halo");
          System.out.println("Connecting...");
  
      } catch (SQLException e) {
          System.out.println("Connection Failed! Check output console");
          e.printStackTrace();
          return;
      }
  
      if (connection != null) {
          System.out.println("You made it, take control your database now!");
      } else {
          System.out.println("Failed to make connection!");
      }
  </code></pre>
  
  <p>The problem is I'm getting this error:</p>
  
  <pre><code>MySQL JDBC Driver Registered!
  Connection Failed! Check output console
  com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 
  
  ** BEGIN NESTED EXCEPTION ** 
  
  java.net.ConnectException
  MESSAGE: Connection refused
  
  STACKTRACE:
  
  java.net.ConnectException: Connection refused
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:382)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:241)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:228)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:431)
      at java.net.Socket.connect(Socket.java:527)
      at java.net.Socket.connect(Socket.java:476)
      at java.net.Socket.&lt;init&gt;(Socket.java:373)
      at java.net.Socket.&lt;init&gt;(Socket.java:216)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:271)
      at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
      at com.mysql.jdbc.Connection.&lt;init&gt;(Connection.java:1555)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
      at java.sql.DriverManager.getConnection(DriverManager.java:582)
      at java.sql.DriverManager.getConnection(DriverManager.java:185)
      at com.main.ParserToDB.main(ParserToDB.java:28)
  
  
  ** END NESTED EXCEPTION **
  
  
  
  Last packet sent to the server was 1 ms ago.
      at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
      at com.mysql.jdbc.Connection.&lt;init&gt;(Connection.java:1555)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
      at java.sql.DriverManager.getConnection(DriverManager.java:582)
      at java.sql.DriverManager.getConnection(DriverManager.java:185)
      at com.main.ParserToDB.main(ParserToDB.java:28)
  </code></pre>
  
  <p>MAMP is configured as follows:
  <img src="https://i.stack.imgur.com/wWgAl.png" alt="mamp configuration"></p>
  
  <p>My question is what could be the problem here?
  Thank you for your help.</p>