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.

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

    ReplyDelete
  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(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
  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')");
    rs.close();
    statement.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    ReplyDelete