We are currently having the same problem as here: How to connect Android Studio with SQL Server database, but it hasn\'t been answered.
We also succeeded the connect
Before you make connection, you must add 2 lines of code:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Complete code as: (may be)
package com.ipvsoft.sendsms.utility;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLConnection {
private static final String LOG = "DEBUG";
private static String ip = "192.168.3.85";
private static String port = "1433";
private static String classs = "net.sourceforge.jtds.jdbc.Driver";
private static String db = "THTData";
private static String un = "sa";
private static String password = "admin";
public static Connection connect() {
Connection conn = null;
String ConnURL = null;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip +":"+port+";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException e) {
Log.d(LOG, e.getMessage());
} catch (ClassNotFoundException e) {
Log.d(LOG, e.getMessage());
}
return conn;
}
}
The error that you are getting is because you are missing the line Class.forName("net.sourceforge.jtds.jdbc.Driver");
Add it before establishing the database connection, so it will look something like this in your code:
String url = "jdbc:jtdc:sqlserver://vps342074.ovh.net/IZY309";
try {
//TextView textViewToChange = (TextView) findViewById(R.id.textViewCol5);
//textViewToChange.setText("Hello");
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pass);
Statement statement = con.createStatement();
ResultSet resultat = statement.executeQuery("SELECT CATEGORIE FROM dbo.PPROFIL WHERE CATEGORIE = 'ac' ");
while (resultat.next()) {
String result = resultat.getString(1);
TextView textViewToChange = (TextView) findViewById(R.id.textViewCol5);
textViewToChange.setText(result);
Log.d("My Custom Tag", result);
}
resultat.close();
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}