This code runs OK in a pure Java application:
Connection conn = null;
Statement inst;
try {
Class.forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance ();
conn = DriverManager.getConnection("jdbc:sqlserver://192.168.1.100\\MSSQLSERVER:1433;DatabaseName=Prueba", "sa", "sa1234");
inst = conn.createStatement();
inst.executeUpdate("INSERT INTO TIENDAS(NOMBRE) VALUES ('ZZZZZZZ') ");
} catch (Exception e) {
e.printStackTrace();
}
}
But in Android project I get the error ‘Socket closed’.
I’m using Eclipse Helios.
Can somebody help me?
Android isn’t a full implementation of Java so this driver, if it is a type 4 driver, could be using some features in Java that haven’t been ported to Android. That’s first and foremost the concern about using code in Android without knowing if it will actually run on Android.
Now a more general concern about what you’re doing. Unless you are developing an SQL browser for android I’d be very concerned about what you are doing. In order for your Android client to see the DB means it has to be exposed over the internet. That’s a seriously bad idea. Exposing your DB to the rest of the world means they don’t have to use your app to connect to it. I can fire up any old SQL browser and start guessing passwords, hacking around, trying default accounts. So if you haven’t locked down your DB it’s not a question of if someone will hack it, but when.
You shouldn’t be directly accessing a remotely accessible DB from Android without going through an APP server like Tomcat, Jetty, etc. Rule of thumb: If you find yourself opening up your DB ports on the firewall to everyone to get your solution to work. You are definitely doing it wrong.