Search code examples
javaandroidsql-serverresultset

Retrieve Data from Resultset using SQL Server in Android


I'm trying to display the data I query from an SQL Server. Please help me.

Here's my code :

final String username = pref.getString("username", ""); // Data from SharedPreferences File
try {
    String query_select = "SELECT * from teller_info where teller_name = '"+username+"'";
    PreparedStatement stmt = connect.prepareStatement(query_select);
    ResultSet rs = stmt.executeQuery();

    while(rs.next()) {
        String teller_id = rs.getString("teller_id");
        TextView view_userID = (TextView) findViewById(R.id._userID);
        view_userID.setText(teller_id);
    }
} catch (SQLException e) {
    e.printStackTrace();
}

My problem is nothing will return. The TextView I set does not change.


Solution

  • Since this is part of an Android App this is not about the data retrieval but displaying that data. As far as I can see the SQL Part seems fine.

    To display data however you would have to add a listener of some sort. That listener would have to update your TextView. Then you would update your View e.g. via notifyDataSetChanged()

    I suggest you go through some tutorials on how to update TextViews - there is a good one on javacodegeeks dot com /android/core/widget/textview/android-textview-example/

    To post a more specific answer I would need more code (ideally the whole activity + layout.xml). It would be good to know when your text should change because a Button would get a onClickListener whereas a TextView could get a AddTextChangedListener.

    Example of a OnClickListener:

    Button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                updateDatabse("some input");
    
                notifyDataSetChanged();
            }
        });