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

ご覧のとおり、のすべての位置に同じオブジェクトがコピーされますArrayListfindList.add(index, finded)(インクリメントindexしている人です)でもやってみましたが、同じ結果になりました。int

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

たぶん、「distinct」の代わりに「groupby」を使用できます。

select .... from ... groupby..。

これを試してみませんか?

于 2012-10-18T08:42:46.947 に答える