title: How to deal with 'CommunicationsException: Communications link failure' in MAMP?
body: <p>I have settled MAMP on my machine and last time when I used that code on my old computer and with another database that works pretty well. Now I cannot understand where I am wrong.</p>
  
  <pre><code>public class JDBC {
  private static String DBURL = "jdbc:mysql://localhost:3306/BookReservationDatabase";
  private static String DBLOGIN = "root";
  private static String DBPASSWORD = "root";
  
  public static void main(String[] args) {
      // Step1: register JDBC driver
      try {
          Class.forName("com.mysql.jdbc.Driver");
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
      // Step 2: Establish the connection to the database
      Connection conn = null;
      Statement stmt = null;
      ResultSet rset = null;
      try {
          conn = DriverManager.getConnection(DBURL, DBLOGIN, DBPASSWORD);
          // Step 3: Create statements (SQL queries)
          stmt = conn.createStatement();
          // Step 4: Execute statements and retrieve results
          rset = stmt.executeQuery("SELECT * FROM Users");
          // Step 5: Analyze results
          while (rset.next()) {
  
          }
      } catch (SQLException e) {
          e.printStackTrace();
      } finally {
          // Step 6: Release resources
          try {
              if (rset != null)
                  rset.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
          try {
              if (stmt != null)
                  stmt.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
          try {
              if (conn != null)
                  conn.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  }
  </code></pre>
  
  <p>My database name is BookReservationDatabase, I use root as a login and password on PhpMyAdmin.</p>
  
  <p>I have got the following errors : </p>
  
  <pre><code>    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.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:342)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:776)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
      at java.sql.DriverManager.getConnection(DriverManager.java:664)
      at java.sql.DriverManager.getConnection(DriverManager.java:247)
      at features.JDBC.main(JDBC.java:26)
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:589)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:301)
      ... 15 more
  </code></pre>
  
  <p>The error must be among DBURL,DBLOGIN and DBPASSWORD. Because the code generate error at the connection  </p>
  
  <pre><code>conn = DriverManager.getConnection(DBURL, DBLOGIN, DBPASSWORD);
  </code></pre>