使用JDBC

时间:2018-03-29 07:53:52

标签: android mysql jdbc

我正在尝试从我的数据库中检索数据并将其填充到listView。但我的代码不起作用,我也尝试添加Log.e,但我没有在logcat中得到任何东西。这是我试图做的:

btw我在这里使用了AsyncTask

onPreExecute():

super.onPreExecute();

doInBackground():

 try {
        Connection con = connectionClass.CONN();
        if (con == null) {
            z = "Please check your internet connection";
        } else {

            String query = "select name from medicines";

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery(query);

            lv.setAdapter(null);

            while (rs.next()) {

                myArrayList.add(rs.getString("name"));
                Log.e("qwerty","working");
            }
        }
    } catch (Exception ex) {
        isSuccess = false;
        z = "Exceptions" + ex;
    }
    return z;

onPostExecute():

ListAdapter listAdapter = new ArrayAdapter<>(MedicineSearch.this, android.R.layout.simple_list_item_1, myArrayList);
        lv.setAdapter(listAdapter);

这就是全部,我的logcat上没有任何内容,这意味着代码不起作用我想,我该如何解决这个问题,还是可以采用另一种方法?

1 个答案:

答案 0 :(得分:0)

您正在返回String&#39; z&#39;来自doInBackground()但您需要返回myArrayList。

try {
    Connection con = connectionClass.CONN();
    if (con == null) {
        z = "Please check your internet connection";
    } else {

        String query = "select name from medicines";

        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(query);

        lv.setAdapter(null);

        while (rs.next()) {

            myArrayList.add(rs.getString("name"));
            Log.e("qwerty","working");
        }
    }
} catch (Exception ex) {
    isSuccess = false;
    z = "Exceptions" + ex;
}
return myArrayList;

然后将调试器放在onPostExecute()中以查看myArrayList是否包含某些数据。 OnPosetExecute()如下所示:

if(myArrayList.size() > 0){
//your set adapter code
}