问题
I am going to develop an Android application for a client. In which the data for the application is saved in the Microsoft SQL Server Database.
Usually I retrieve from MySQL database with PHP as webserver and parsed with json, I am not having hard at it. I don't have any clue to connect with Microsoft SQL Server Database.
I googled several times, but there is no good suggestion. Have any of you worked on it? Or if you have any ideas please share with me.
回答1:
To access it directly, you can use a JDBC driver.
http://jtds.sourceforge.net/
Download the jar file here:
http://sourceforge.net/projects/jtds/
Sample code:
public void ConnectToDatabase(){
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
String username = "XXX";
String password = "XXX";
Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://192.188.1.1:1433/DATABASE;user=" + username + ";password=" + password);
Log.w("Connection","open");
Statement stmt = DbConn.createStatement();
ResultSet reset = stmt.executeQuery(" select * from users ");
EditText num = (EditText) findViewById(R.id.displaymessage);
num.setText(reset.getString(1));
DbConn.close();
} catch (Exception e)
{
Log.w("Error connection","" + e.getMessage());
}
}
来源:https://stackoverflow.com/questions/21979473/how-to-access-microsoft-sql-server-database-for-android-application