title: Could not create connection to database server - JDBC Driver in Android Studio
body: <p>java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.</p>
  <p>Hello i am trying to connect to a database on my android application, i have run the exact same code in normal intellij and other programs before and it connects but this time it does not want to connect.
  I have tried different databases different Source/Target Compatibility versions.
  I added the JBC library into the module but no luck.</p>
  <p>Anyone has an idea?</p>
  <p>this is the code</p>
  <pre><code>        try {
          if (connection != null &amp;&amp; !connection.isClosed()) {
              return;
          }
          String url = &quot;jdbc:mysql://localhost/test?autoReconnect=true&amp;useSSL=false&quot;;
          String user = &quot;root&quot;;
          String password = &quot;password&quot;;
          Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
          connection = DriverManager.getConnection(url, user, password);
          statement = connection.createStatement();
      } catch (SQLException | ClassNotFoundException e) {
          e.printStackTrace();
      }
  </code></pre>
  <p>2020-07-01 08:52:23.348 2785-2785/com.jends.mcshopplus W/System.err: Loading class <code>com.mysql.jdbc.Driver'. This is deprecated. The new driver class is </code>com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
  2020-07-01 08:52:27.654 2785-2785/com.jends.mcshopplus W/System.err: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
  2020-07-01 08:52:27.655 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
  2020-07-01 08:52:27.655 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
  2020-07-01 08:52:27.655 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
  2020-07-01 08:52:27.655 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
  2020-07-01 08:52:27.655 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
  2020-07-01 08:52:27.655 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:906)
  2020-07-01 08:52:27.655 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:831)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:580)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:218)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at com.jends.mcshopplus.MySQLData.openConnection(MySQLData.java:23)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at com.jends.mcshopplus.MainActivity.onCreate(MainActivity.java:24)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.Activity.performCreate(Activity.java:7957)
  2020-07-01 08:52:27.656 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.Activity.performCreate(Activity.java:7946)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3530)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3707)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2220)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:107)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.os.Looper.loop(Looper.java:237)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:8016)
  2020-07-01 08:52:27.657 2785-2785/com.jends.mcshopplus W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  2020-07-01 08:52:27.658 2785-2785/com.jends.mcshopplus W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
  2020-07-01 08:52:27.658 2785-2785/com.jends.mcshopplus W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
  2020-07-01 08:52:27.659 2785-2785/com.jends.mcshopplus W/System.err: Caused by: android.os.NetworkOnMainThreadException
  2020-07-01 08:52:27.659 2785-2785/com.jends.mcshopplus W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
  2020-07-01 08:52:27.659 2785-2785/com.jends.mcshopplus W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
  2020-07-01 08:52:27.659 2785-2785/com.jends.mcshopplus W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
  2020-07-01 08:52:27.660 2785-2785/com.jends.mcshopplus W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:1152)
  2020-07-01 08:52:27.660 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132)
  2020-07-01 08:52:27.660 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
  2020-07-01 08:52:27.660 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
  2020-07-01 08:52:27.660 2785-2785/com.jends.mcshopplus W/System.err:     at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:850)
  2020-07-01 08:52:27.660 2785-2785/com.jends.mcshopplus W/System.err:    ... 23 more</p>