title: Java: Unable to access the remote MySQL database using JDBC
body: <p>I am trying to access the MySQL database, which is installed in my Web server, using my Java code. My Java application is a Swing application, running in my PC. Below is my Java code.</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.io.PrintStream;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import java.util.prefs.Preferences;
  
  
  public class TestDatabase {
  
      private Connection con;
  
      private void connect()
      {
          try
          {
               Class.forName("com.mysql.jdbc.Driver");
  
  
               con = DriverManager.getConnection("jdbc:mysql://72.0.0.0:3306/aceko_test","aceko_me","rtf7890");
  
               System.out.println("Connection Established");
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
  
      public static void main(String[]args)
      {
          new TestDatabase().connect();
      }
  
  }
  </code></pre>
  
  <p>I am getting the below error.</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:408)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:358)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2498)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2535)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2320)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:834)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:46)
      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:408)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
      at java.sql.DriverManager.getConnection(DriverManager.java:664)
      at java.sql.DriverManager.getConnection(DriverManager.java:247)
      at TestDatabase.connect(TestDatabase.java:31)
      at TestDatabase.main(TestDatabase.java:43)
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:589)
      at java.net.Socket.connect(Socket.java:538)
      at java.net.Socket.&lt;init&gt;(Socket.java:434)
      at java.net.Socket.&lt;init&gt;(Socket.java:244)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:308)
      ... 16 more
  </code></pre>
  
  <p>I have done my knowledge best in <code>CPanel</code> of my web server to assign the remote access. As you can see in below image, I have provided the "Any Access"</p>
  
  <p><img src="https://i.stack.imgur.com/RXjxA.png" alt="enter image description here"></p>
  
  <p>As you can see in the below image, I have created the database, user, and assigned the user to the database.</p>
  
  <p><img src="https://i.stack.imgur.com/TfaJW.png" alt="enter image description here"></p>
  
  <p>Please note the IP I have mentioned in my code is fake, only for the use of SO.</p>