2

I know that the JDBC is best used with a low latency high-bandwidth connection. and it is better to use a php service.

Thing is am developing a Restaurant menu app that should retrieve data from a db located on a server. using the Samsung galaxy tab2 7inch as the target device and having a stable wireless connection with high bandwidth.My first attempt was to have a java server running on the same machine that have the db and have alot of threads that will retrieve data and send them to android device over a tcp socket.

This implementation resulted in high cpu usage both on the host machine and the android device since i have a lot of threads working in the background.

I though to give JDBC a try since the environment enable me to use such implementation. but so far not successful with connecting to the DB.even tho i have internet permission on the android application Manifest.

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();
        }

    }

this code will give me the following error

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

As for the PHP web-service i really dont know anything how to use it or setup it. what do you guys think i should do ? continue with my first implementation or switch to a direct approach from the android device to the mysql db using a php service .

Sorry This is considered a duplicate . i just want your opinion about the problem am having and if someone could point me to a better approach or can help with the error from the JDBC i will be thankful.

Ibrahem Ahmed
  • 265
  • 1
  • 4
  • 13

1 Answers1

1

Only yesterday did another user ask the same thing and there are some good answers to be found on that question - JDBC vs Web Service for Android

As for the PHP web-service i really dont know anything how to use it or setup it.

I guess you're gonna have to take a 101 on that.

Community
  • 1
  • 1
Deepak Bala
  • 11,095
  • 2
  • 38
  • 49