3

i am new in android.. i want to connect sql server using JTDS driver. can any one tell me..

thnx in advance...

Piyush
  • 3,061
  • 8
  • 34
  • 52

4 Answers4

6

Getting error "ClassNotFoundException" while using JTDS on ANDROID to direct access SQLSERVER?

After 3 hours RND, for finding solution for the same above error. I didnt get there is no error in the code, also I have import "jtds-1.3.0" library properly continues debugging of code still getting same error again and again.

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

 Connection conn = DriverManager.getConnection(
 db_connect_string, db_userid, db_password);
}

I tried alternative to, changing line

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

to

...... (new Driver()).getClass();

...... (new Driver())

when I tried all of these, I tought there might be a problem in jtds-1.3.0 library, and what I did, just download the old version jtds-1.2.5 and import it; and the issue resolved.

So, friends out there If you getting same error and tried different ways already, try this.

KumailR
  • 505
  • 1
  • 4
  • 20
  • 2
    Note i tried 'jtds-1.3.0', and test it by making JAVA APPLICATION, It worked properly, but its not working with ANDROID... Done know why! – KumailR Nov 22 '12 at 11:49
  • the same error i had, i used 1.3.0 and make java application its worked, but not working on android, so i download jtds-1.2.5.jar and replace with jtds-1.3.0. and it worked. – KumailR Jan 07 '13 at 12:04
3

It's weird that there is no example code on the jTDS website. I found this, it might be helpfull:

http://www.java-tips.org/other-api-tips/jdbc/how-to-connect-microsoft-sql-server-using-jdbc-3.html

import java.sql.*;

public class testConnection
{
    public static void main(String[] args) 
    {
        DB db = new DB();
        db.dbConnect("jdbc:jtds:sqlserver://localhost:1433/tempdb","sa","");
    }
}

class DB
{
    public DB() {}

    public voidn dbConnect(String db_connect_string, String db_userid, String db_password)
    {
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
            db_connect_string, db_userid, db_password);
            System.out.println("connected");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
};

EDIT:
You will get ClassNotFoundException exception when your main class cannot be found. Find the following lines in your AndroidManifest.xml make sure they are correct:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ezee.app"  
/*...*/
<activity android:name=".connect12" 

Also make sure that the class exists at your_project_folder/src/com/ezee/app/connect12 (case sensitive I think)

Caner
  • 57,267
  • 35
  • 174
  • 180
0

in my experience, if you're using Android with a stand-alone installation of SQL Server, you must use 10.0.2.2 address instead of "localhost" or "127.0.0.1", according to Android specifics for accessing localhost servers.

I tried so, and have successfully connected to my SQL Server.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
0
Exception in thread "main" java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.test.objectref.GroupBy.main(GroupBy.java:12)

To solve this issue had to add Jtds lib.

Rob
  • 4,927
  • 12
  • 49
  • 54