2

我知道 JDBC 最适合与低延迟高带宽连接一起使用。最好使用 php 服务。

事情正在开发一个餐厅菜单应用程序,它应该从位于服务器上的数据库中检索数据。使用三星galaxy tab2 7inch作为目标设备并具有高带宽的稳定无线连接。我的第一次尝试是让Java服务器在具有数据库的同一台机器上运行,并且有很多线程可以检索数据并发送它们通过 tcp 套接字到 android 设备。

此实现导致主机和 android 设备上的 cpu 使用率很高,因为我有很多线程在后台工作。

我想尝试一下 JDBC,因为环境使我能够使用这样的实现。但到目前为止,连接到 DB 并没有成功。即使我在 android 应用程序清单上有 Internet 权限。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            this.setTitle("Driver started ");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.1.3/cafe", "root", "1234");
            this.setTitle("Connected to db");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

这段代码会给我以下错误

04-07 15:22:41.222: W/System.err(11318): com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
04-07 15:22:41.222: W/System.err(11318): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
04-07 15:22:41.238: W/System.err(11318):    at java.lang.reflect.Constructor.constructNative(Native Method)
04-07 15:22:41.238: W/System.err(11318):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-07 15:22:41.238: W/System.err(11318):    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
04-07 15:22:41.238: W/System.err(11318):    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
04-07 15:22:41.238: W/System.err(11318):    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
04-07 15:22:41.238: W/System.err(11318):    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
04-07 15:22:41.238: W/System.err(11318):    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
04-07 15:22:41.245: W/System.err(11318):    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
04-07 15:22:41.245: W/System.err(11318):    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
04-07 15:22:41.245: W/System.err(11318):    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
04-07 15:22:41.245: W/System.err(11318):    at java.lang.reflect.Constructor.constructNative(Native Method)
04-07 15:22:41.245: W/System.err(11318):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-07 15:22:41.245: W/System.err(11318):    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
04-07 15:22:41.245: W/System.err(11318):    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
04-07 15:22:41.245: W/System.err(11318):    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
04-07 15:22:41.245: W/System.err(11318):    at java.sql.DriverManager.getConnection(DriverManager.java:175)
04-07 15:22:41.245: W/System.err(11318):    at java.sql.DriverManager.getConnection(DriverManager.java:209)
04-07 15:22:41.245: W/System.err(11318):    at com.example.anndroidjdbc.MainActivity.onCreate(MainActivity.java:23)
04-07 15:22:41.245: W/System.err(11318):    at android.app.Activity.performCreate(Activity.java:4465)
04-07 15:22:41.245: W/System.err(11318):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
04-07 15:22:41.245: W/System.err(11318):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
04-07 15:22:41.245: W/System.err(11318):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
04-07 15:22:41.245: W/System.err(11318):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
04-07 15:22:41.245: W/System.err(11318):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
04-07 15:22:41.245: W/System.err(11318):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 15:22:41.245: W/System.err(11318):    at android.os.Looper.loop(Looper.java:137)
04-07 15:22:41.253: W/System.err(11318):    at android.app.ActivityThread.main(ActivityThread.java:4514)
04-07 15:22:41.253: W/System.err(11318):    at java.lang.reflect.Method.invokeNative(Native Method)
04-07 15:22:41.253: W/System.err(11318):    at java.lang.reflect.Method.invoke(Method.java:511)
04-07 15:22:41.253: W/System.err(11318):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-07 15:22:41.253: W/System.err(11318):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-07 15:22:41.253: W/System.err(11318):    at dalvik.system.NativeStart.main(Native Method)
04-07 15:22:41.253: W/System.err(11318): Caused by: java.net.SocketException: android.os.NetworkOnMainThreadException
04-07 15:22:41.253: W/System.err(11318):    at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:410)
04-07 15:22:41.253: W/System.err(11318):    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:271)
04-07 15:22:41.253: W/System.err(11318):    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
04-07 15:22:41.253: W/System.err(11318):    ... 27 more

至于 PHP 网络服务,我真的不知道如何使用或设置它。你们觉得我应该怎么做?继续我的第一个实现,或者使用 php 服务切换到从 android 设备到 mysql db 的直接方法。

抱歉 这被认为是重复的。我只是希望您对所遇到的问题提出意见,如果有人可以指出更好的方法或可以帮助解决 JDBC 的错误,我将不胜感激。

4

1 回答 1

1

就在昨天,另一个用户问了同样的问题,并且在这个问题上找到了一些很好的答案 - JDBC vs Web Service for Android

至于 PHP 网络服务,我真的不知道如何使用或设置它。

我猜你得考101

于 2013-04-07T13:35:10.610 回答