在最后一次迭代中迭代JDBC结果集重复结果

时间:2016-01-24 18:34:01

标签: java mysql arrays jdbc

正如标题所说,这是我的代码:

String query = "SELECT * FROM " + TABLE_UNIVERSITY_INFO + " WHERE university_code = ?";
    Connection conn = DriverManager.getConnection(DATABASE_SCHEMA, USERNAME, PASSWORD);

    PreparedStatement stmt = conn.prepareStatement(query);
    stmt.setString(1, universityCode);
    ResultSet rs = stmt.executeQuery();
    while (rs.next()) {
            // Get DTO from database
            summonerUniversityDTO.setUniversityName(rs.getString("university_code"));
            summonerUniversityDTO.setID(rs.getLong("summonerId"));
            summonerUniversityDTO.setSummonerName(rs.getString("summoner_name"));

            //Add all to array
            summonerUniversityDTOArray.add(summonerUniversityDTO);
            logger.info("ARRAY" + summonerUniversityDTOArray);
            count++;
        }
        logger.info(String.format("[%s] summoners were returned from the database matching university code [%s]", count, universityCode));
        universitySummonerDTO.setSingleSummonerPlayerDTOs(summonerUniversityDTOArray);
        logger.info(String.format("UniversityArrayDTO is [%s]", universitySummonerDTO));

在我的数据库中,我有3行将被返回,每行都有不同的名称和UID - ripolly,kaibai和lol。

它获得了所有结果,但是当我点击我的终端并查看返回的内容时:

2016-01-24 18:25:31 INFO  DatabaseAccessor:201 - Returning University DTO from g
iven university name: [SOME]

2016-01-24 18:25:31 INFO  DatabaseAccessor:219 - ARRAY[SummonerUniversityDTO{sum
monerID=155767, summonerName='lol', universityName='SOME'}]

2016-01-24 18:25:31 INFO  DatabaseAccessor:219 - ARRAY[SummonerUniversityDTO{sum
monerID=37656713, summonerName='kaibai', universityName='SOME'}, SummonerUnivers
ityDTO{summonerID=37656713, summonerName='kaibai', universityName='SOME'}]

2016-01-24 18:25:31 INFO  DatabaseAccessor:219 - ARRAY[SummonerUniversityDTO{sum
monerID=38584682, summonerName='ripolly', universityName='SOME'}, SummonerUniver
sityDTO{summonerID=38584682, summonerName='ripolly', universityName='SOME'}, Sum
monerUniversityDTO{summonerID=38584682, summonerName='ripolly', universityName='
SOME'}]

2016-01-24 18:25:31 INFO  DatabaseAccessor:222 - [3] summoners were returned fro
m the database matching university code [SOME]

2016-01-24 18:25:31 INFO  DatabaseAccessor:224 - UniversityArrayDTO is [Universi
tySummonerDTO{singleSummonerPlayerDTOs=[SummonerUniversityDTO{summonerID=3858468
2, summonerName='ripolly', universityName='SOME'}, SummonerUniversityDTO{summone
rID=38584682, summonerName='ripolly', universityName='SOME'}, SummonerUniversity
DTO{summonerID=38584682, summonerName='ripolly', universityName='SOME'}]}]

名称在数组中输入正确,但它们在arraylist中重复出现。这是我的代码的问题,还是我迭代它/结果集游标的方式等等。坚持这一段时间!

道歉我的命名惯例并不简单......

干杯

2 个答案:

答案 0 :(得分:1)

你没有在while循环的每次迭代中创建一个新对象,因此它只是编辑已经在数组中的同一个对象并将其添加到第二次和第三次。

while循环应该类似于:

while (rs.next()) {
        summonerUniversityDTO = new SummonerUniversityDTO();
        // Get DTO from database
        summonerUniversityDTO.setUniversityName(rs.getString("university_code"));
        summonerUniversityDTO.setID(rs.getLong("summonerId"));
        summonerUniversityDTO.setSummonerName(rs.getString("summoner_name"));

        //Add all to array
        summonerUniversityDTOArray.add(summonerUniversityDTO);
        logger.info("ARRAY" + summonerUniversityDTOArray);
        count++;
    }

答案 1 :(得分:0)

我在我的while循环之外实例化sed -n '/foo/ { :a n /foo/q p ba }' infile - 而不是在其中:

summonerUniversityDTO

而不是它:

while (rs.next()) {
        // Get DTO from database
        SummonerUniversityDTO summonerUniversityDTO = new SummonerUniversityDTO();
        summonerUniversityDTO.setUniversityName(rs.getString("university_code"));
        summonerUniversityDTO.setID(rs.getLong("summonerId"));
        summonerUniversityDTO.setSummonerName(rs.getString("summoner_name"));