1

i have java class in which i have done database connection for SQL server and trying to retrieve data from table its working fine. but now i created web service in java for that class then its showing me ClassNotFoundException :net.sourceforge.jtds.jdbc.Driver i have import external jar as well. Actually i need to show data from database to Android layout that's why i created web service which will helpful to retrieve data from database. But when i am trying to retrieve data from database then Drivers are not loading.its showing above error. i also tried like following :

//DatabaseConnetivityClass.java

public class DatabaseConnetivityClass 
    {
     public static void main(String Args[])
     {
           new DatabaseConnetivityClass().getData();
     }
     public String getData()
     {
       String s = null;
       try {
        s = new MainConnection().getData();
       } catch (SQLException e) {           
        e.printStackTrace();
    }
    return s;
  }
 }

MainConnection.java

public class MainConnection {
Connection con;
public MainConnection() {
    try{
    Class.forName("net.sourceforge.jtds.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/databasename","username","password");
    }
    catch (Exception e) {
        System.out.println("exp:"+e);
    }
}

public String getData() throws SQLException {
    StringBuffer sb = new StringBuffer();
    Statement select = con.createStatement();
    ResultSet result = select.executeQuery("SELECT * FROM Personal_Info");
    while (result.next()) {
        // process results one row at a time

        String val = result.getString(1);
        sb.append(val);
        System.out.println("val = " + val);
    }
    select.close();
    return sb.toString();
}}

i thought it will work but its giving the same error. So please help me if anybody knows the solution

swan
  • 21
  • 2
OnkarDhane
  • 1,450
  • 2
  • 14
  • 24
  • 1
    looks like you imported the .jar incorrectly. – zapl Mar 12 '12 at 11:13
  • @zapl if i didn't import .jar correctly then how it's running successfully for simple java file. when i am trying to execute through web service then it's giving error – OnkarDhane Mar 12 '12 at 11:41
  • Maybe you did not import the jar into the webservice? `ClassNotFoundException :net.sourceforge.jtds.jdbc.Driver` basically says that Java can't find that class anywhere inside your app .apk. Check e.g. http://stackoverflow.com/questions/1334802/how-can-i-use-external-jars-in-an-android-project how to import the jar – zapl Mar 12 '12 at 12:47
  • btw: `Class.forName("net.sourceforge.jtds.jdbc.Driver");` does not look very useful since you don't use the returned `Class` Object. If you import the library you can directly use `import net.sourceforge.jtds.jdbc.Driver;` and reference the Class later as usual. – zapl Mar 12 '12 at 12:50

0 Answers0