Reputation: 439
I've been trying to connect from android to Microsoft SQL server's database. But i'm not able to do it. Please help. I've tried so many things. I was using jtds - 1.3.1 i tried using 1.3.0 version also. But still failed. Please help.
public class ConnectionClass {
String ip = "192.168.0.101\\SQLEXPRESS:1433";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "dictionary";
String un = "";
String password = "";
@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return conn;
}
}
and i'm getting this error:
E/ERRO: Unknown server host name 'Host is unresolved: 192.168.0.101\SQLEXPRESS'.
I've also tried removing SQLEXPRESS(like this: 192.168.0.101:1033) from above line but then another error is coming.
I've enabled TCP/IP from MS SQL and i've also allowed port 1433 from firewall as well as i've also allowed MS SQL from firewall. MS SQL's allow remote connection is also checked.
Please help. Thanks..
Upvotes: 0
Views: 1757
Reputation: 11
This is not true address '192.168.0.101\SQLEXPRESS:1433' just go on like this '192.168.0.101'
Do not try it on your device(sometimes it cant see the server's ip) , firstly try it with any 'Emulator' also dont forget to make network settings on VirtualBox
Upvotes: 0
Reputation: 93559
That isn't a valid url. You'd need to use either the IP alone, or scheme://ip. However it is NOT recommended to directly connect to a db from a mobile device- doing so requires your password to be in the app, which means anyone who uses the app has access to your password. Use a web service instead and keep the password on your server only for security.
Also, never set the strict mode. First off its a hack. Secondly it doesn't work on all devices. Third of all, if you need it you're going to leave your app unresponsive and quite possibly crashing due to watchdog timers. ANy site you saw using it doesn't know what they're doing.
Upvotes: 1