0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Noufal
  • 439
  • 1
  • 13
  • 35
  • 1
    This is a popular question on stackoverflow, please see: http://stackoverflow.com/questions/3492417/connect-to-sql-server-from-android – Steven Trigg Feb 24 '14 at 05:24

1 Answers1

3

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());
        }
}
live-love
  • 48,840
  • 22
  • 240
  • 204