gpt4 book ai didi

java - 迭代 JDBC 结果集重复上次迭代的结果

转载 作者:行者123 更新时间:2023-11-29 11:45:47 25 4
gpt4 key购买 nike

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

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'}]}]

名称已正确输入到数组中,但它们在数组列表中重复出现。这是我的代码的问题,还是我迭代它/结果集游标等的问题。在这个问题上停留了一段时间!

抱歉,我的命名约定并不简单......

干杯

最佳答案

您不会在 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++;
}

关于java - 迭代 JDBC 结果集重复上次迭代的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34979765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com