Android JDBC driver for Microsoft SQL Server and Sybase


Recently i shared a post about Configuring and accessing MySQL jdbc driver on android application.

Good thing about MySQL is that it is open source and hence the driver is open source too. Even if we couldn’t find any android compatible driver, it wouldn’t be too difficult to get the source code and port it for android.

But when it comes to MSSQL and Sybase, its a different story. After successfully configuring Mysql JDBC driver I was looking for compatible drivers for MSSQL and unfortunately none of the versions available were compatible.

It was disappointing in start but then I started looking for some hacks around and came across a really wonderful library – jTDS JDBC Driver.

Now good things about this driver is that it is free software available under terms of GNU Lesser General Public License. Which means it is allowed to be used in open source as well as comercial projects.

Another good thing about this JDBC driver is that it supports both MSSQL and Sybase and it is compatible with Android.

jTDS is an open source 100% pure Java (type 4) JDBC 3.0 driver for Microsoft SQL Server (6.5, 7, 2000, 2005 and 2008) and Sybase (10, 11, 12, 15). jTDS is based on FreeTDS and is currently the fastest production-ready JDBC driver for SQL Server and Sybase. jTDS is 100% JDBC 3.0 compatible, supporting forward-only and scrollable/updateable ResultSets, concurrent (completely independent) Statements and implementing all the DatabaseMetaData and ResultSetMetaData methods. Check out the feature matrix for more details.

Quite a few of the commercial JDBC drivers out there are based on jTDS (or FreeTDS), even if they no longer acknowledge this. jTDS has been tested with virtually all JDBC-based database management tools and is the driver of choice for most of these (recommended for DbVisualizer and SQuirreL SQL, distributed with Aqua Data Studio and DataDino). jTDS is also becoming a common choice for enterprise-level applications: it passes both the J2EE 1.3 certification and Hibernate test suites, and is recommended for JBoss, Hibernate, Atlassian JIRA and Confluence and Compiere.

So go ahead and give it a shot.. It worked for me, I am sure you will enjoy working with it too..

PS: Have a look at FAQs before jumping into writing code.

Advertisements

19 comments

    1. please tell are you sure this work with you in android apps
      and why not work with me
      he working under JSE no problem but under android not work this not for android

      1. I just checked I was using jtds-1.2.5.jar and here is a code snippet from my android sample.

        private void testDB() {
        TextView tv = (TextView) this.findViewById(R.id.text_view);
        try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String connectionUrl = "jdbc:jtds:sqlserver://<ip-address>:<port>/<dbname>;user=<user>;password=<password>;socketTimeout=200";
        Connection con = DriverManager.getConnection(connectionUrl);
        String result = "Database connection success\n";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from tblblock1");
        ResultSetMetaData rsmd = rs.getMetaData();
        while (rs.next()) {
        result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
        result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
        result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
        }
        tv.setText(result);
        } catch (Exception e) {
        e.printStackTrace();
        }
        }

    2. please tell are you sure this work with you in android apps
      and why not work with me
      he working under JSE no problem but under android not work this not for android
      by logic is not designed for working android
      android just linux kernel not more

    1. Just check the build path. I am sure its just build path issue. The JAR should be added to your build path. Also I remember my eclipse was behaving crazy for some time.. All I had to kill it and start again to make it realize that my build path is correct.

      Kill eclipse, start again, fix your build path, and clean build the application.

      1. im added to build bath and i take this code copy and past
        are you add any think to manifest file ?
        i added to libs and to build path and added this jar to all folder in my project why give me exception on Class.forName(“net.sourceforge.jtds.jdbc.Driver”);
        ClassNotFoundException
        if you know any steps for that please please tell my

      2. i had created a folder named “lib” in my project and then placed this jar into it.

        Came to project in eclipse and refreshed it. It showed the lib folder and JAR inside.

        Then i right clicked on JAR and clicked on “Build Path” > “Add To Build Path”

        Have you done the same thing?

      3. thanks for you
        i have libs in project is added automatically from android and
        any way i created lib and added
        try {
        Class.forName(“net.sourceforge.jtds.jdbc.Driver”);
        Toast.makeText(getBaseContext(), “work”, Toast.LENGTH_LONG).show();

        } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }

        error message net.sourceforge.jtds.jdbc.Driver

      4. i need develop this apps please share your project
        or tell may anyway to solve
        Class.forName(“net.sourceforge.jtds.jdbc.Driver”);

    1. hi dear! will you please tell me how you connected to database and how it is working i need a complete guide line if you please help me…

  1. hey, I have the same problem… can you help me? the problem in this line he can’t find
    Class.forName(“net.sourceforge.jtds.jdbc.Driver”);

    1. Hi Yudi,

      As I mentioned earlier, if the JAR file is properly included in project and class name mentioned in source code is correct then there is no reason it would not work…

      Just refresh your eclipse and try again. it should work.

  2. Dear respected sir, i tried your given code as mentioned i do all the task and follow your path but my database is not connected it jumps to the catch statement i want to know that who can i connected to my database i configure the SQL2012 TCP ip as enable and from last 30 days i’m trying to connect it but i’m failed please its my humble request to help me out this i have to use this but app is stuck. and how can i get the ip address of SQL which i have to provide in connection string? here is the code that you provided with my changing

    TextView tv = (TextView) this.findViewById(R.id.textView1);
    try {
    Class.forName(“net.sourceforge.jtds.jdbc.Driver”);
    String connectionUrl = “jdbc:jtds:sqlserver://192.168.1.2:1433/ECP;user=MyLogin;password=*****;socketTimeout=200”;
    Connection con = DriverManager.getConnection(connectionUrl);
    String result = “Database connection success\n”;
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(“select * from test”);
    ResultSetMetaData rsmd = rs.getMetaData();
    while (rs.next()) {
    result += rsmd.getColumnName(1) + “: ” + rs.getInt(1) + “\n”;
    result += rsmd.getColumnName(2) + “: ” + rs.getString(2) + “\n”;
    }
    tv.setText(result);
    } catch (Exception e) {
    e.printStackTrace();
    tv.setText(“Not Connected”);
    }

    it is not connected. if you please guide me on skype or teamviewer then it will be a grateful to me i’m a student and stuck at this point please help me i’ll be very thankful to you. i’m waiting for your help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s