title: jdbc : Could not create connection to database server
body: <p>I am trying to connect mysql database using jdbc.
  Here is my Database connection Class:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Database {
      String userName = "myUserName";
      String password = "myPass";
  
      String url = "jdbc:mysql://xxx.xxx.xxx.xxx:3306/database_Name?autoReconnect=true&amp;useSSL=false";
  
      public void connect()
      {
          try {
              Class.forName("com.mysql.jdbc.Driver").newInstance();
  
              Connection conn = DriverManager.getConnection(url,userName,password);
  
              Statement statement = conn.createStatement();
              String queryString = "select * from users";
              ResultSet rs = statement.executeQuery(queryString);
              while (rs.next()) {
                  System.out.println(rs.getString(2));
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>For now, you can assume that I am calling that method like:</p>
  
  <pre><code>new Database().connect();
  </code></pre>
  
  <p>And I get this exception:</p>
  
  <pre class="lang-java prettyprint-override"><code>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
      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.Util.getInstance(Util.java:408)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
      at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2163)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2088)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:806)
      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:410)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
      at java.sql.DriverManager.getConnection(DriverManager.java:664)
      at java.sql.DriverManager.getConnection(DriverManager.java:247)
      at Database.&lt;init&gt;(Database.java:17)
      at ServerTest.main(ServerTest.java:8)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  Caused by: 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:989)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:341)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
      at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2104)
      ... 19 more
  Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
      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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      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:300)
      ... 21 more
  </code></pre>
  
  <p>*I added % to access database from remote.</p>
  
  <p>*Java part working for localhost but when I try this remote database, it fails.</p>
  
  <p>*The database and tables are created.(there is no problem about this.)</p>
  
  <p>So where is the problem?</p>