I'm recently doing an android app that need data exchange with MSSQL database. What I'm doing now is using a PHP page to "connect" my app and the database. Just wondering is there a way to let the android app sending queries directly to the database? If there is a way to do so, where can I find some examples?
Asked
Active
Viewed 1,988 times
1
-
I do not think sending direct sql queries is a good idea. Please look at this [answer](http://stackoverflow.com/questions/13471822/why-dont-connect-android-to-database-directly) – Michal Jan 14 '13 at 16:13
-
No, you are doing right. – Irfan DANISH Jan 14 '13 at 16:13
-
1AFAIK no. That's what server-side scripting is used for to communicate with database from your front-end. – Kanth Jan 14 '13 at 16:13
-
2Theoretically it IS possible if the Android device is on the same network as the DB and you loaded (or implemented) the appropriate driver. (That also assumes there is an ODBC or MSSQL for Android driver available) However, as others have mentioned it is not advisable since it builds a hard coupling of your app to the DB. Any slight change to the DB schema or network infrastructure would require a redeployment of all installed clients whereas using a PHP middle tier would only require a single point of change. – Cliff Jan 14 '13 at 16:18
2 Answers
1
No, you are doing right. You have to create web services or API that your android application will use to interact with your database.

Irfan DANISH
- 8,349
- 12
- 42
- 67
0
Yes it is possible by using "net.sourceforge.jtds.jdbc.Driver".
Just a simple example:
Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
//test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
String connString = "jdbc:jtds:sqlserver://10.2.0.0/DatabaseName;encrypt=fasle;user=xxxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
String username = "your username";
String password = "your password";
conn = DriverManager.getConnection(connString,username,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from dbo.Account");
while(reset.next()){
Log.w("Data:",reset.getString(3));
Log.w("Data",reset.getString(2));
}
conn.close();
} catch (Exception e)
{
Log.w("Error connection","" + e.getMessage());
}
DatabaseName
}

Ranjit
- 5,130
- 3
- 30
- 66