简体   繁体   中英

How Connect To Remote MySQL server via JDBC from Android Applicaton

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.

Question What is wrong with my code that it does not connect ? I used a similar process in Java and it works beautifully.

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.

2) Below is the code to connect and query the database. I left out query info and server info, replaced with < >. Obviously :P

3) When I run the app on my phone I get this message:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure.

4) I run wireshark on Ubuntu server and just see SSTP entries when it listens on mysql port.

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://<server ip>:<sql port#>/<database name>";
    private static final String user = "<username>";
    private static final String pass = "<password>";

    @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 <table in database>");
            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

5) Stack Trace:

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.<init>(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.<init>(ConnectionImpl.java:792)
03-20 11:50:51.596: W/System.err(23747):    at com.mysql.jdbc.JDBC4Connection.<init>(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.<init>(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() !

There are two things why it isn't working here

The jdbc is not able to hit at the url you are giving it,the URL must be accessible publically that means your server ie:SAY xyzw:port

so need a network guy to free up this port on the server ip and make it available publically

CAUTION

Opening this publically will cause your database a threat as it can be written to by anyone who knows it..

A WORD OF ADVICE NEVER allow your app to directly communicate with the database,allow a server technology like jsp,servlets,asp,python etc etc to do receive your request and do that for you.

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM