Java アプリケーションでリモートの MySQL サーバーに接続する方法を理解しました。私はこれをよく知っています。私は自分のコードと、Android アプリケーションから同じことを行うために行った手順を提供しました。
質問接続しないコードの何が問題になっていますか? 私はJavaで同様のプロセスを使用しましたが、うまく機能します。
1)プロジェクトのプロパティにmysql-connector.jarの外部JARファイルを追加しました(5.1を使用したと思います)。Javaアプリで使用していて、接続しているため、これは正常に機能します。
2) 以下は、データベースに接続してクエリを実行するためのコードです。クエリ情報とサーバー情報を省略し、< > に置き換えました。明らかに:P
3) 携帯電話でアプリを実行すると、次のメッセージが表示されます。
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信リンクの失敗。
4) Ubuntu サーバーで Wireshark を実行すると、mysql ポートでリッスンするときに SSTP エントリが表示されます。
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) スタック トレース:
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() !