How to Connect SQL Server Using Jtds Driver in Android

how to connect sql server using JTDS driver in Android

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.

android - sql server - connection either null or no suitable driver found

@NiravMadariya, The JDBC connection string with jTDS shoule be jdbc:jtds:sqlserver://<server>:<port>/<database>, please see the jTDS FAQ which includes URL format and the reason for No suitable driver exception.

Note, if you are using the JTDS JDBC driver for Auzre SQL DB, then you will need to add ssl=require to the URL of the connection string as like this jdbc:jtds:sqlserver://<server>:<port>/<database>?ssl=require.

Meanwhile, using Microsoft JDBC Driver for SQL Server is a recommend way for Azure SQL DB, please see https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-java-simple/.

Sql Database connect Using the JDBC Driver with android

Change the Dirver Name and before import the lib file jtds 1.3.0.jar file.and change below the code it will work fine.............

public List<String> dbConnect(String Host, String Port, String db_userid,
String db_password) {
List<String> Db_list = new ArrayList<String>();
try {
String ConnectionString = "jdbc:jtds:sqlserver://" + Host + ":"
+ Port;
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(ConnectionString,
db_userid, db_password);
System.out.println("connected");
Statement statement = conn.createStatement();
String queryString = "select name from sys.databases";
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
Db_list.add(rs.getString(1));
}
} catch (Exception e) {
Db_list.add("Error");
e.printStackTrace();
}
return Db_list;
}


Related Topics



Leave a reply



Submit