0

要将数据库中的不同对象添加到一个,ArrayList这是我使用的代码:

try {
    Statement stat = con.createStatement();
    ResultSet rs = stat.executeQuery("SELECT DISTINCT Id, Username, FirstName, LastName FROM USER WHERE Username LIKE '%"+name+"%' or FirstName LIKE '%"+name+"%' or LastName LIKE '%"+name+"%'");
    while (rs.next()){
        finded.setUsername(rs.getString("Username"));
        finded.setFirstName(rs.getString("FirstName"));
        finded.setLastName(rs.getString("LastName"));
        finded.setId(rs.getInt("Id"));
        findList.add(finded);
        Log.d("this is the content of the List:",((Integer) findList.get(j).getId()).toString());
        Log.d("this is the content of the List 0:",((Integer) findList.get(0).getId()).toString());

    }
    con.close();

这就是 Log.d 每次都给我的。

10-18 08:31:39.407: D/this is the content of the List:(427): 3
10-18 08:31:39.407: D/this is the content of the List 0:(427): 3
10-18 08:31:39.427: D/this is the content of the List:(427): 15
10-18 08:31:39.427: D/this is the content of the List 0:(427): 15
10-18 08:31:39.437: D/this is the content of the List:(427): 13
10-18 08:31:39.437: D/this is the content of the List 0:(427): 13
10-18 08:31:39.447: D/this is the content of the List:(427): 50
10-18 08:31:39.447: D/this is the content of the List 0:(427): 50
10-18 08:31:39.460: D/this is the content of the List:(427): 34
10-18 08:31:39.460: D/this is the content of the List 0:(427): 34
10-18 08:31:39.467: D/this is the content of the List:(427): 49
10-18 08:31:39.467: D/this is the content of the List 0:(427): 49
10-18 08:31:39.479: D/this is the content of the List:(427): 53
10-18 08:31:39.479: D/this is the content of the List 0:(427): 53

如您所见,它在ArrayList. 我也尝试使用findList.add(index, finded)( indexis an intwho is incrementing) 但我得到了相同的结果。

4

4 回答 4

4

您需要finded在循环内部创建一个新对象

while (rs.next()){
    finded = new Finded();   // <<-- here
    finded.setUsername(rs.getString("Username"));

否则你只是一遍又一遍地添加同一个对象

于 2012-10-18T08:44:37.667 回答
2

更改为以下内容:

 int j=0;
 try {
                Statement stat = con.createStatement();
                ResultSet rs = stat.executeQuery("SELECT DISTINCT Id, Username, FirstName, LastName FROM USER WHERE Username LIKE '%"+name+"%' or FirstName LIKE '%"+name+"%' or LastName LIKE '%"+name+"%'");
                while (rs.next()){
                    Finded finded=new Finded();
                    finded.setUsername(rs.getString("Username"));
                    finded.setFirstName(rs.getString("FirstName"));
                    finded.setLastName(rs.getString("LastName"));
                    finded.setId(rs.getInt("Id"));
                    findList.add(finded);
                    Log.d("this is the content of the List:",((Integer) findList.get(j).getId()).toString());
                    Log.d("this is the content of the List 0:",((Integer) findList.get(0).getId()).toString());
j++;
                }
                con.close();
于 2012-10-18T08:46:55.390 回答
1

您的查询返回多条记录,但是您正在覆盖该finded对象,而不是在循环中创建一个新记录。在循环中创建一个新对象

Finded finded =null;
while (rs.next()){
                    finded = new Finded();
                    finded.setUsername(rs.getString("Username"));
                    finded.setFirstName(rs.getString("FirstName"));
                    finded.setLastName(rs.getString("LastName"));
                    finded.setId(rs.getInt("Id"));
                    findList.add(finded);


                }
于 2012-10-18T08:51:27.130 回答
0

也许你可以使用“group by”而不是“distinct”。

select .... from ... group by ...

试试这个怎么样?

于 2012-10-18T08:42:46.947 回答