【发布时间】:2020-11-05 09:18:39
【问题描述】:
我有一个“购物车”按钮,用于查询 select * 并在 ArrayList 中添加项目(文章),因为我想避免每次单击按钮时都在列表中添加文章
我试过这个:
for(Article a : articleList){
String checkId = "select * from article";
PreparedStatement ps = conn.prepareStatement(checkId);
ResultSet rs = ps.executeQuery(checkId);
rs.next();
if(a.getId()==rs.getInt("id")){
//Here i'm returning list, just like it is in DataBase.
} else{
//Here i want to add articles in the list
}
}
我假设我遇到了那个异常,因为我在遍历同一个列表时在 ArrayList 中添加了文章。我该如何解决这个问题?
【问题讨论】:
-
将它们添加到另一个
List。 -
您只查看查询返回的第一行。也许你应该把你的 SQL 改成 'select count(*) from article where id = ?',然后 ps.setInteger(a.getId())
标签: java for-loop exception arraylist