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

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


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

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

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

    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

    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.

      This is as simple as explained in the above link.

  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

  3. 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.(
    06-19 10:33:29.077: W/System.err(15447): at net.sourceforge.jtds.jdbc.Driver.connect(
    06-19 10:33:29.077: W/System.err(15447): at java.sql.DriverManager.getConnection(
    06-19 10:33:29.077: W/System.err(15447): at java.sql.DriverManager.getConnection(
    06-19 10:33:29.077: W/System.err(15447): at com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask.doInBackground(
    06-19 10:33:29.077: W/System.err(15447): at com.alkber.androidmssql2008.datastore.Dao$DBConnectionTask.doInBackground(
    06-19 10:33:29.078: W/System.err(15447): at android.os.AsyncTask$
    06-19 10:33:29.080: W/System.err(15447): at
    06-19 10:33:29.080: W/System.err(15447): at android.os.AsyncTask$SerialExecutor$
    06-19 10:33:29.080: W/System.err(15447): at java.util.concurrent.ThreadPoolExecutor.runWorker(
    06-19 10:33:29.080: W/System.err(15447): at java.util.concurrent.ThreadPoolExecutor$
    06-19 10:33:29.080: W/System.err(15447): at
    06-19 10:33:29.081: W/System.err(15447): Caused by: Host is unresolved: 103
    06-19 10:33:29.082: W/System.err(15447): at
    06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(
    06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.SharedSocket.(
    06-19 10:33:29.082: W/System.err(15447): at net.sourceforge.jtds.jdbc.JtdsConnection.(
    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

    1. Try replacing your host name with ip address

  4. 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.

  5. 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(;
    b.setOnClickListener(new View.OnClickListener()
    public void onClick(View arg0) {

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

  6. 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')");

    } catch (SQLException e) {




  7. Halo sir ,

    I want to learn how to connect sql to my android app.I downloaded eg from "git clone" 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.


Blog Archive