Wednesday, October 30, 2013

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, though it seems like a bug, go to order and Export tab and enable jtds


Now, for the database connection , all network operations should be in different thread, ie 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 clone git@github.com:alkber/AndroidByExample.git

Or you can download my AndroidByExample [ZIP] git repo.

11 comments:

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

    ReplyDelete
    Replies
    1. This comment has been removed by a blog administrator.

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

      Delete
    3. This comment has been removed by the author.

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

      Delete
    5. 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
    6. 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. This comment has been removed by the author.

    ReplyDelete
  3. 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
  4. i did not get the result it just failed to connect i used your code with my address but it is not connecting here is the code please help me out i am waitng...thanks

    @Override
    public void onResume() {

    super.onResume();
    dao.connect("192.168.1.5:1433", "awais", "MyLogin", "ECP");

    }

    i just change this address to your provided code but its not working please help...

    ReplyDelete
  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

    ReplyDelete