Android mysql-connector-java Library

时间:2014-05-31 17:48:12

标签: android mysql jdbc libraries mysql-connector

我试图在我的Android应用程序和Mysql Server之间建立连接。

我有两个应用程序和完全相同的代码。一个应用程序完美地连接,但另一个应用程序使Comunications链接失败。

此代码位于onCreate方法:

    Log.d("Trying to instantiate driver", "try");
    try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    this.setTitle("Driver instantiated");

                    try {
                        conexionMySQL = DriverManager.getConnection("jdbc:mysql://"+ip+":"+port,user,pass);
                        Log.d("CONNECTION", "connected");
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Log.d("CONNECTION", "not connected: "+e.getMessage());
                    }


            } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                Log.d("com.mysql.jdbc.Driver", "NOT found");
                    this.setTitle(".....Class com.mysql.jdbc.Driver not found!");
                    e.printStackTrace();
            } catch (IllegalAccessException e) {
                Log.d("com.mysql.jdbc.Driver", "ilegal access");
                    this.setTitle("Illegal access");
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (InstantiationException e) {
                Log.d("com.mysql.jdbc.Driver", "Instantiation exception");
                    this.setTitle("instantiation exc eption");
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }

在工作应用程序中,我得到了以下日志:

05-31 19:17:47.105: D/Trying to instantiate driver(17103): try
05-31 19:17:53.635: D/CONNECTION(17103): connected

在无效的应用程序上,我收到了以下日志:

05-31 19:21:43.395: D/Trying to instantiate driver(18088): try
05-31 19:21:43.795: W/dalvikvm(18088): VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
05-31 19:21:43.795: I/dalvikvm(18088): Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom
05-31 19:21:43.795: W/dalvikvm(18088): VFY: unable to resolve virtual method 16799: Ljavax/naming/Reference;.get (Ljava/lang/String;)Ljavax/naming/RefAddr;
05-31 19:21:43.795: D/dalvikvm(18088): VFY: replacing opcode 0x6e at 0x0004
05-31 19:21:43.795: W/dalvikvm(18088): VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
05-31 19:21:43.795: E/dalvikvm(18088): Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
05-31 19:21:43.795: W/dalvikvm(18088): VFY: unable to resolve new-instance 1537 (Ljavax/naming/StringRefAddr;) in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;
05-31 19:21:43.795: D/dalvikvm(18088): VFY: replacing opcode 0x22 at 0x0006
05-31 19:21:43.795: D/dalvikvm(18088): DexOpt: unable to opt direct call 0x41a1 at 0x14 in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;.storeTo
05-31 19:21:43.835: I/dalvikvm(18088): Could not find method java.lang.management.ManagementFactory.getThreadMXBean, referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation
05-31 19:21:43.835: W/dalvikvm(18088): VFY: unable to resolve static method 16114: Ljava/lang/management/ManagementFactory;.getThreadMXBean ()Ljava/lang/management/ThreadMXBean;
05-31 19:21:43.835: D/dalvikvm(18088): VFY: replacing opcode 0x71 at 0x0079
05-31 19:21:43.895: W/System.err(18088): com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
05-31 19:21:43.895: W/System.err(18088): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
05-31 19:21:43.895: W/System.err(18088):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-31 19:21:43.895: W/System.err(18088):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
05-31 19:21:43.895: W/System.err(18088):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-31 19:21:43.895: W/System.err(18088):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
05-31 19:21:43.895: W/System.err(18088):    at java.sql.DriverManager.getConnection(DriverManager.java:175)
05-31 19:21:43.895: W/System.err(18088):    at java.sql.DriverManager.getConnection(DriverManager.java:209)
05-31 19:21:43.895: W/System.err(18088):    at com.unilocation.unilocation.MainActivity.onCreate(MainActivity.java:169)
05-31 19:21:43.895: W/System.err(18088):    at android.app.Activity.performCreate(Activity.java:5191)
05-31 19:21:43.895: W/System.err(18088):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
05-31 19:21:43.895: W/System.err(18088):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
05-31 19:21:43.895: W/System.err(18088):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
05-31 19:21:43.895: W/System.err(18088):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
05-31 19:21:43.895: W/System.err(18088):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
05-31 19:21:43.895: W/System.err(18088):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 19:21:43.895: W/System.err(18088):    at android.os.Looper.loop(Looper.java:137)
05-31 19:21:43.895: W/System.err(18088):    at android.app.ActivityThread.main(ActivityThread.java:4872)
05-31 19:21:43.895: W/System.err(18088):    at java.lang.reflect.Method.invokeNative(Native Method)
05-31 19:21:43.895: W/System.err(18088):    at java.lang.reflect.Method.invoke(Method.java:511)
05-31 19:21:43.895: W/System.err(18088):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-31 19:21:43.895: W/System.err(18088):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-31 19:21:43.895: W/System.err(18088):    at dalvik.system.NativeStart.main(Native Method)
05-31 19:21:43.895: W/System.err(18088): Caused by: java.net.SocketException: android.os.NetworkOnMainThreadException
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:410)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:271)
05-31 19:21:43.895: W/System.err(18088):    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
05-31 19:21:43.895: W/System.err(18088):    ... 27 more
05-31 19:21:43.895: D/CONNECTION(18088): not connected: Communications link failure
05-31 19:21:43.895: D/CONNECTION(18088): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

工作库中的mysql-connector-java-5.1.24-bin正确显示:

image1

但是不工作的库上的mysql-connector-java-5.1.24-bin有一个小图标:

image2

图书馆有什么问题?

1 个答案:

答案 0 :(得分:5)

Caused by: java.net.SocketException: android.os.NetworkOnMainThreadException

是问题的原因。你不能在Android的UI线程中进行网络i / o(至少从API级别11开始 - 以前可能,但非常气馁)。

您需要使用AsyncTaskThread或任何其他类似机制将此逻辑移至后台线程。