Monday, February 13, 2017

Android and Microsoft SQL (MS SQL SERVER 2008) marriage

All you have to do is use the appropriate driver, being a opensource fan , i'd recommend using JTDS.

Instructions below assumes you are using





  • Android Developer Tools Build id: v22.0.1-685705,
  • Eclipse Version: 4.2.1.v20130118 Build id: M20130204-1200
  • JTDS 1.2.5 

  • Though things will work out other versions higher than this and possibly lower as well , its just to ensure a known state where things worked out.

    So, once the JTDS is jar is download, first step is to include jar to your build path





    Next, it seems like a bug, go to order and Export tab and enable jtds




    Since the database connection operation is networked related, all network operations should be in different thread away from the main UI thread,  so use  AsyncTask.

    Working code is can be downloaded from here , read the code, it is well documented.


    git@github.com:alkber/AndroidByExample.git
    

    17 comments :

    1. I can't get data from MSSQL2008 ....
      I got error ' null '

      ReplyDelete
      Replies
      1. it been a year right know lol , can you share with us the code about how to connect android to MSSQL2008 ?

        Delete
      2. @Mohamed Brahen can you ask your doubts specifically from the above code ? The code is quite sefl explanatory. Please be free to ask.

        Delete
      3. Thank you very much for the reply, i was asking about how to connect a file php to SQL SERVER 2008 I'd tried all the exemple but nothing happened ,this thing kill me

        Delete
      4. Its always better to make use of RESTApi to access your database contents, so that data access becomes transparent [hiding the connection details etc] . However for php case, i suppose your are talking to MSSQL server on the same server, ie php file and database on same machine.

        http://www.w3schools.com/php/php_mysql_connect.asp

        This is as simple as explained in the above link.

        Delete
    2. My english isn't good enough to understand this article and I need this example I build an And. App but I need to connect my server who can help me abouth this

      ReplyDelete
    3. I have attached the JAR file with the application.. but still I am getting this error:

      06-18 18:43:00.572: I/System.out(9470): [CDS][DNS]Unable to resolve host "103": No address associated with hostname
      06-18 18:43:00.573: I/com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask(9470): connecting failed
      06-18 18:43:00.573: W/System.err(9470): java.sql.SQLException: Unknown server host name 'Host is unresolved: 103'.
      06-18 18:43:00.584: W/System.err(9470): at net.sourceforge.jtds.jdbc.JtdsConnection.(JtdsConnection.java:427)
      06-18 18:43:00.584: W/System.err(9470): at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
      06-18 18:43:00.585: W/System.err(9470): at java.sql.DriverManager.getConnection(DriverManager.java:175)
      06-18 18:43:00.585: W/System.err(9470): at java.sql.DriverManager.getConnection(DriverManager.java:209)
      06-18 18:43:00.586: W/System.err(9470): at com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask.doInBackground(Dao.java:81)
      06-18 18:43:00.586: W/System.err(9470): at com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask.doInBackground(Dao.java:1)
      06-18 18:43:00.587: W/System.err(9470): at android.os.AsyncTask$2.call(AsyncTask.java:287)
      06-18 18:43:00.588: W/System.err(9470): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
      06-18 18:43:00.588: W/System.err(9470): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
      06-18 18:43:00.590: W/System.err(9470): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
      06-18 18:43:00.592: W/System.err(9470): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
      06-18 18:43:00.592: W/System.err(9470): at java.lang.Thread.run(Thread.java:838)
      06-18 18:43:00.593: W/System.err(9470): Caused by: java.net.UnknownHostException: Host is unresolved: 103
      06-18 18:43:00.594: W/System.err(9470): at java.net.Socket.connect(Socket.java:836)
      06-18 18:43:00.595: W/System.err(9470): at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
      06-18 18:43:00.596: W/System.err(9470): at net.sourceforge.jtds.jdbc.SharedSocket.(SharedSocket.java:251)
      06-18 18:43:00.597: W/System.err(9470): at net.sourceforge.jtds.jdbc.JtdsConnection.(JtdsConnection.java:331)
      06-18 18:43:00.597: W/System.err(9470): ... 11 more
      06-18 18:43:00.597: E/com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask(9470): Unknown server host name 'Host is unresolved: 103'.
      06-18 18:43:00.626: I/SurfaceTextureClient(9470): [STC::queueBuffer] (this:0x5279c278) fps:0.29, dur:3485.55, max:3485.55, min:3485.55

      Can you please help..

      ReplyDelete
    4. I am trying out with your example but I am constantly getting this error:
      ------------------------------------------------------------------------------------------------------
      06-19 10:33:29.070: I/System.out(15447): [CDS][DNS]Unable to resolve host "103": No address associated with hostname
      06-19 10:33:29.073: I/com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask(15447): connecting failed
      06-19 10:33:29.074: W/System.err(15447): java.sql.SQLException: Unknown server host name 'Host is unresolved: 103'.
      06-19 10:33:29.077: W/System.err(15447): at net.sourceforge.jtds.jdbc.JtdsConnection.(JtdsConnection.java:427)
      06-19 10:33:29.077: W/System.err(15447): at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
      06-19 10:33:29.077: W/System.err(15447): at java.sql.DriverManager.getConnection(DriverManager.java:175)
      06-19 10:33:29.077: W/System.err(15447): at java.sql.DriverManager.getConnection(DriverManager.java:209)
      06-19 10:33:29.077: W/System.err(15447): at com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask.doInBackground(Dao.java:81)
      06-19 10:33:29.077: W/System.err(15447): at com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask.doInBackground(Dao.java:1)
      06-19 10:33:29.078: W/System.err(15447): at android.os.AsyncTask$2.call(AsyncTask.java:287)
      06-19 10:33:29.080: W/System.err(15447): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
      06-19 10:33:29.080: W/System.err(15447): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
      06-19 10:33:29.080: W/System.err(15447): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
      06-19 10:33:29.080: W/System.err(15447): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
      06-19 10:33:29.080: W/System.err(15447): at java.lang.Thread.run(Thread.java:838)
      06-19 10:33:29.081: W/System.err(15447): Caused by: java.net.UnknownHostException: Host is unresolved: 103
      06-19 10:33:29.082: W/System.err(15447): at java.net.Socket.connect(Socket.java:836)
      06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
      06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.SharedSocket.(SharedSocket.java:251)
      06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.JtdsConnection.(JtdsConnection.java:331)
      06-19 10:33:29.082: W/System.err(15447): ... 11 more
      06-19 10:33:29.082: E/com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask(15447): Unknown server host name 'Host is unresolved: 103'.
      ---------------------------------------------------------------------------------------------------------------------
      I think the JARfile is not getting connected with the

      ReplyDelete
      Replies
      1. Try replacing your host name with ip address

        Delete
    5. I am trying out with your example but I am constantly getting this error:
      ------------------------------------------------------------------------------------------------------
      06-19 10:33:29.070: I/System.out(15447): [CDS][DNS]Unable to resolve host "103": No address associated with hostname
      06-19 10:33:29.073: I/com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask(15447): connecting failed
      06-19 10:33:29.074: W/System.err(15447): java.sql.SQLException: Unknown server host name 'Host is unresolved: 103'.
      06-19 10:33:29.077: W/System.err(15447): at net.sourceforge.jtds.jdbc.JtdsConnection.(JtdsConnection.java:427)
      06-19 10:33:29.077: W/System.err(15447): at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
      06-19 10:33:29.077: W/System.err(15447): at java.sql.DriverManager.getConnection(DriverManager.java:175)
      06-19 10:33:29.077: W/System.err(15447): at java.sql.DriverManager.getConnection(DriverManager.java:209)
      06-19 10:33:29.077: W/System.err(15447): at com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask.doInBackground(Dao.java:81)
      06-19 10:33:29.077: W/System.err(15447): at com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask.doInBackground(Dao.java:1)
      06-19 10:33:29.078: W/System.err(15447): at android.os.AsyncTask$2.call(AsyncTask.java:287)
      06-19 10:33:29.080: W/System.err(15447): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
      06-19 10:33:29.080: W/System.err(15447): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
      06-19 10:33:29.080: W/System.err(15447): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
      06-19 10:33:29.080: W/System.err(15447): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
      06-19 10:33:29.080: W/System.err(15447): at java.lang.Thread.run(Thread.java:838)
      06-19 10:33:29.081: W/System.err(15447): Caused by: java.net.UnknownHostException: Host is unresolved: 103
      06-19 10:33:29.082: W/System.err(15447): at java.net.Socket.connect(Socket.java:836)
      06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
      06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.SharedSocket.(SharedSocket.java:251)
      06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.JtdsConnection.(JtdsConnection.java:331)
      06-19 10:33:29.082: W/System.err(15447): ... 11 more
      06-19 10:33:29.082: E/com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask(15447): Unknown server host name 'Host is unresolved: 103'.
      ---------------------------------------------------------------------------------------------------------------------
      I think the JARfile is not getting connected with the Application :(

      ReplyDelete
    6. Hi, I tried using your sample code but whenever I launch the application, the system keep giving me this error "Charset 0x0904000128/Cp850 is not supported by the JVM"I had tried connecting to the database in Java and it works fine but only in java android it gives me error . please help.

      ReplyDelete
    7. Hello, I used your sample code, the code woking very well. I try to use dao.addUser(); function onclick button listener. the program doesnt work gives an error "unfortunely,the program has stopped".how can I solve this problem could you help please, thanks for sharing your interest.
      Button b =(Button)findViewById(R.id.btn1);
      b.setOnClickListener(new View.OnClickListener()
      {
      @Override
      public void onClick(View arg0) {
      dao.addUser();
      }
      });

      Note: dao.addUser(); function working well public void onConnectionSuccessful() { dao.addUser(); } but it doenst work on button click event.

      ReplyDelete
    8. Replies
      1. Kindly how to add login screen with dao.adduer like dao.checkuser()

        Delete
    9. public void addUser(/* Some user info Model as parameter */) {

      Log.i(LOG_TAG, "in adduser()");

      if (conn == null) {

      throw new InstantiationError(
      "call Dao.connect(...) before calling Dao operations");

      }

      try {

      Statement statement = conn.createStatement();
      ResultSet rs = statement
      .executeQuery("INSERT INTO user_info_table "
      + " VALUES ('1001', 'Bob', '333333', '33')");
      rs.close();
      statement.close();

      } catch (SQLException e) {

      e.printStackTrace();

      }

      }

      ReplyDelete
    10. Halo sir ,

      I want to learn how to connect sql to my android app.I downloaded eg from "git clone git@github.com:alkber/AndroidByExample.git" but when i run the app i'm getting output as
      attempting to connect to xx.xx.x....1433
      with username :user
      with password :password
      I'm a beginner to android.

      ReplyDelete
    11. Halo sir ,

      I want to learn how to connect sql to my android app.I downloaded eg from "git clone git@github.com:alkber/AndroidByExample.git" but when i run the app i'm getting output as
      attempting to connect to xx.xx.x....1433
      with username :user
      with password :password
      I'm a beginner to android.

      ReplyDelete