Shawtari
Shawtari

Reputation: 31

Connecting and creating an android application to retrieve data from MSSQL

Am kind of new to android.the thing is i programmed a vb code to upload some data to SQL server including an image.so i want to create an android app to view these data by creating a drop down list with the main entry in the database..so whenever i choose something from the drop down list it suppose to show the rest of the columns..sorry for my English but i hope u guys got the idea.

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  public void query2() {
    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:// AMD/SQLEXPRESS :1433/***********;encrypt=fasle;user=*******;password=*********;instance=SQLEXPRESS;";
      String username = "Shawtari";
      String password = "";
      conn = DriverManager.getConnection(connString,username,password);
      Log.w("Connection","open");
      Statement stmt = conn.createStatement();
      ResultSet forest = stmt.executeQuery("select * from aspnet_Users");

      //Print the data to the console
      while(forest.next()){
        Log.w("username",forest.getString(3));
      }
      conn.close();
    } catch (Exception e) {
      Log.w("Error connection","" + e.getMessage());
    }
  }
}

Upvotes: 2

Views: 1274

Answers (1)

oks16
oks16

Reputation: 1294

  1. Use a Spinner to show your main entries and a ListView to show the columns
  2. Add a OnItemSelectedListener for this spinner. In this listener, make the JDBC call as a background task preferably using AsyncTask.
  3. After getting the response, populate the listview . You may need to implement a custom adapter which takes the ResultSet and parse it to feed it to your listview.

Upvotes: 1

Related Questions