Need help! Connecting MS SQL Server 2005 to Eclipse

852 Views Asked by At

I'm a newbie with database programming in android and SQL Server. I'm doing a project and i'm having problems with it, I tried so many sample codes but i couldn't get my attempt to be successful. So please bear with my inexperience.

This is my code.

package com.example.testing;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Main extends Activity {

     private static final String url = "jdbc:jtds:sqlserver://127.0.0.1:1433/qqq";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testDB();
    }


    public void testDB() {
        TextView tv = (TextView)this.findViewById(R.id.textView1);
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection con = DriverManager.getConnection(url);
            /* System.out.println("Database connection success"); */

            String result = "Database connection success\n";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from tblname");
            ResultSetMetaData rsmd = rs.getMetaData();

            while(rs.next()) {
                result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";

            }
            tv.setText(result);
        }
        catch(Exception e) {
            e.printStackTrace();
            tv.setText(e.toString());
        }   
    }
}

Error message:

java.lang.ClassNotFoundException:net.sourceforge.jtds.jdbc.Driver
1

There are 1 best solutions below

0
On

You have to download the jTDS JDBC driver and add it to your classpath. You are loading this driver here:

Class.forName("net.sourceforge.jtds.jdbc.Driver");

but it seems not to be available at runtime.