简体   繁体   English

在Android中使用SQL Server从Resultset检索数据

[英]Retrieve Data from Resultset using SQL Server in Android

I'm trying to display the data I query from an SQL Server. 我正在尝试显示从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. 我设置的TextView不变。

Since this is part of an Android App this is not about the data retrieval but displaying that data. 由于这是Android应用程序的一部分,因此这与数据检索无关,而是显示数据。 As far as I can see the SQL Part seems fine. 据我所见,SQL部分似乎还不错。

To display data however you would have to add a listener of some sort . 要显示数据,您必须添加某种侦听器 That listener would have to update your TextView. 该侦听器必须更新您的TextView。 Then you would update your View eg via notifyDataSetChanged() 然后,您将更新视图,例如通过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/ 我建议您阅读一些有关如何更新TextView的教程-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). 要发布更具体的答案,我需要更多代码(最好是整个活动+ 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. 最好知道何时更改文本,因为Button可以获取onClickListener,而TextView可以获取AddTextChangedListener。

Example of a OnClickListener: OnClickListener的示例:

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

            notifyDataSetChanged();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM