title: Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
body: <p>I have created a database at php-myadmin and I want to create a connection between database and java, for that I have written this code.</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  
  
  public class B {
  
      public static void main(String arg[]) throws Exception {
  
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection("jdbc:mysql://mysql87928-jahanzeb.j.layershift.co.uk/jahanzeb","root","**********");
          PreparedStatement statement = con.prepareStatement("select * from school");
          ResultSet result = statement.executeQuery();
  
          while(result.next())
              System.out.printf("%-5s%-5s%-5s",result.getString(1),result.getString(2),result.getString(3));
  
      }
  
  }
  </code></pre>
  
  <p>but I am getting an error after running this, please tell how to solve this error?</p>
  
  <pre><code>Exception in thread "main" 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(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1036)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:338)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2232)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:790)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:44)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at B.main(B.java:12)
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:297)
      ... 15 more
  </code></pre>