如何访问Android应用程序的Microsoft SQL Server数据库?

时间:2014-02-24 05:21:28

标签: android android-webservice

我将为客户开发Android应用程序。其中应用程序的数据保存在Microsoft SQL Server数据库中。

通常我使用PHP作为网络服务器从MySQL数据库中检索并使用json解析,我并不是很努力。我没有任何线索可以连接Microsoft SQL Server数据库。

我用Google搜索过几次,但没有好的建议。有没有人参与其中?或者,如果您有任何想法,请与我分享。

1 个答案:

答案 0 :(得分:2)

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());
        }
}