title: How Connect To Remote MySQL server via JDBC from Android Applicaton
body: <p>I figured out how to connect to my remote MySQL server in a Java application. I know this very well. I have provided my code and the steps I took to do the same thing but from an Android Application.</p>
  
  <p><strong>Question</strong> What is wrong with my code that it does not connect ? I used a similar process in Java and it works beautifully.</p>
  
  <p>1) I added the External JAR file of mysql-connector.jar in the project properties ( I used 5.1 i believe). This works fine because I use it in my Java app and it connects with it.</p>
  
  <p>2) Below is the code to connect and query the database. I left out query info and server info, replaced with &lt; >. Obviously :P</p>
  
  <p>3) When I run the app on my phone I get this message:</p>
  
  <p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure.</p>
  
  <p>4) I run wireshark on Ubuntu server and just see SSTP entries when it listens on mysql port.</p>
  
  <pre><code>package com.example.test_android;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.Statement;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  
      private static final String url = "jdbc:mysql://&lt;server ip&gt;:&lt;sql port#&gt;/&lt;database name&gt;";
      private static final String user = "&lt;username&gt;";
      private static final String pass = "&lt;password&gt;";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
           testDB();
  
  
      }//end oncreate method
  
  
      public void testDB() {
          TextView tv = (TextView)this.findViewById(R.id.text_view);
          try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection(url, user, pass);
              /* System.out.println("Database connection success"); */
  
              String result = "Database connection success\n";
              Statement st = con.createStatement();
              ResultSet rs = st.executeQuery("SELECT * FROM &lt;table in database&gt;");
              ResultSetMetaData rsmd = rs.getMetaData();
  
              while(rs.next()) {
                  result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
                  result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
                  result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
              }
              tv.setText(result);
          }
          catch(Exception e) {
              e.printStackTrace();
              tv.setText(e.toString());
          }   
  
      }
  
  }//end class
  </code></pre>
  
  <p>5) Stack Trace:</p>
  
  <pre><code>03-20 11:50:46.381: W/SurfaceFlinger(334): id=54501 Removed idx=5 Map Size=4
  03-20 11:50:46.381: D/memalloc(334): ion: Freeing buffer base:0x42fb0000 size:1228800 fd:50
  03-20 11:50:46.381: D/memalloc(334): ion: Freeing buffer base:0x43107000 size:1228800 fd:11
  03-20 11:50:46.381: W/SurfaceFlinger(334): id=54501 Removed idx=-2 Map Size=4
  03-20 11:50:46.381: D/memalloc(334): ion: Freeing buffer base:0x43c2d000 size:1228800 fd:53
  03-20 11:50:46.381: D/KeyguardViewMediator(648): setHidden false
  03-20 11:50:47.302: E/MP-Decision(1366): DOWN Ld:51 Ns:1.100000 Ts:190 rq:1.000000 seq:196.000000
  03-20 11:50:47.492: D/STATUSBAR-NetworkController(805): onReceive() - RSSI_CHANGED_ACTION, WIFI_STATE, NETWORK_STATE
  03-20 11:50:48.143: E/SMD(330): DCD ON
  03-20 11:50:48.603: V/WindowOrientationListener(648): nearestRotation : 0   Angle: 348   tilt: 45
  03-20 11:50:50.535: D/STATUSBAR-NetworkController(805): onReceive() - RSSI_CHANGED_ACTION, WIFI_STATE, NETWORK_STATE
  03-20 11:50:50.555: E/MP-Decision(1366): UP Ld:50 Nw:1.990000 Tw:140 rq:2.700000 seq:147.000000
  03-20 11:50:50.946: W/dalvikvm(23747): VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
  03-20 11:50:50.946: I/dalvikvm(23747): Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom
  03-20 11:50:50.946: W/dalvikvm(23747): VFY: unable to resolve virtual method 11306: Ljavax/naming/Reference;.get (Ljava/lang/String;)Ljavax/naming/RefAddr;
  03-20 11:50:50.946: D/dalvikvm(23747): VFY: replacing opcode 0x6e at 0x0004
  03-20 11:50:50.946: W/dalvikvm(23747): VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
  03-20 11:50:50.946: E/dalvikvm(23747): Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
  03-20 11:50:50.946: W/dalvikvm(23747): VFY: unable to resolve new-instance 944 (Ljavax/naming/StringRefAddr;) in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;
  03-20 11:50:50.946: D/dalvikvm(23747): VFY: replacing opcode 0x22 at 0x0006
  03-20 11:50:50.956: D/dalvikvm(23747): DexOpt: unable to opt direct call 0x2c2c at 0x14 in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;.storeTo
  03-20 11:50:51.146: E/SMD(330): DCD ON
  03-20 11:50:51.476: I/dalvikvm(23747): Could not find method java.lang.management.ManagementFactory.getThreadMXBean, referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation
  03-20 11:50:51.476: W/dalvikvm(23747): VFY: unable to resolve static method 10657: Ljava/lang/management/ManagementFactory;.getThreadMXBean ()Ljava/lang/management/ThreadMXBean;
  03-20 11:50:51.476: D/dalvikvm(23747): VFY: replacing opcode 0x71 at 0x0079
  03-20 11:50:51.576: W/System.err(23747): com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  03-20 11:50:51.576: W/System.err(23747): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  03-20 11:50:51.576: W/System.err(23747):    at java.lang.reflect.Constructor.constructNative(Native Method)
  03-20 11:50:51.576: W/System.err(23747):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
  03-20 11:50:51.576: W/System.err(23747):    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
  03-20 11:50:51.586: W/System.err(23747):    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
  03-20 11:50:51.586: W/System.err(23747):    at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:344)
  03-20 11:50:51.586: W/System.err(23747):    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
  03-20 11:50:51.586: W/System.err(23747):    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
  03-20 11:50:51.586: W/System.err(23747):    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
  03-20 11:50:51.586: W/System.err(23747):    at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:792)
  03-20 11:50:51.596: W/System.err(23747):    at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
  03-20 11:50:51.596: W/System.err(23747):    at java.lang.reflect.Constructor.constructNative(Native Method)
  03-20 11:50:51.596: W/System.err(23747):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
  03-20 11:50:51.596: W/System.err(23747):    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
  03-20 11:50:51.596: W/System.err(23747):    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
  03-20 11:50:51.596: W/System.err(23747):    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
  03-20 11:50:51.606: W/System.err(23747):    at java.sql.DriverManager.getConnection(DriverManager.java:175)
  03-20 11:50:51.606: W/System.err(23747):    at java.sql.DriverManager.getConnection(DriverManager.java:209)
  03-20 11:50:51.606: W/System.err(23747):    at com.example.test_android.MainActivity.testDB(MainActivity.java:34)
  03-20 11:50:51.606: W/System.err(23747):    at com.example.test_android.MainActivity.onCreate(MainActivity.java:24)
  03-20 11:50:51.606: W/System.err(23747):    at android.app.Activity.performCreate(Activity.java:4470)
  03-20 11:50:51.616: W/System.err(23747):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
  03-20 11:50:51.616: W/System.err(23747):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
  03-20 11:50:51.616: W/System.err(23747):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
  03-20 11:50:51.616: W/System.err(23747):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
  03-20 11:50:51.616: W/System.err(23747):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
  03-20 11:50:51.616: W/System.err(23747):    at android.os.Handler.dispatchMessage(Handler.java:99)
  03-20 11:50:51.626: W/System.err(23747):    at android.os.Looper.loop(Looper.java:137)
  03-20 11:50:51.626: W/System.err(23747):    at android.app.ActivityThread.main(ActivityThread.java:4517)
  03-20 11:50:51.626: W/System.err(23747):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-20 11:50:51.626: W/System.err(23747):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-20 11:50:51.626: W/System.err(23747):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
  03-20 11:50:51.626: W/System.err(23747):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
  03-20 11:50:51.636: W/System.err(23747):    at dalvik.system.NativeStart.main(Native Method)
  03-20 11:50:51.636: W/System.err(23747): Caused by: java.net.SocketException: android.os.NetworkOnMainThreadException
  03-20 11:50:51.636: W/System.err(23747):    at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:408)
  03-20 11:50:51.636: W/System.err(23747):    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:269)
  03-20 11:50:51.646: W/System.err(23747):    at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:294)
  03-20 11:50:51.646: W/System.err(23747):    ... 28 more
  03-20 11:50:51.786: W/SurfaceFlinger(334): id=54502(1) createSurface 0x1b75144 (1x1),2 flag=400
  03-20 11:50:51.786: D/KeyguardViewMediator(648): setHidden false
  03-20 11:50:51.786: D/STATUSBAR-StatusBarManagerService(648): setSystemUiVisibility(0x0)
  03-20 11:50:51.786: D/STATUSBAR-StatusBarManagerService(648): manageDisableList what=0x0 pkg=WindowManager.LayoutParams
  03-20 11:50:51.867: D/CLIPBOARD(1073): Hide Clipboard dialog at Starting input: finished by someone else... !
  03-20 11:50:51.867: I/ClipboardServiceEx(648): mCBPickerDialog enter case. MSG_DISMISS_DIALOG
  03-20 11:50:51.867: I/ClipboardServiceEx(648): Send intent for dismiss clipboard dialog inside hideCurrentInputLocked() !
  </code></pre>