我正在创建一种算法,该算法从数据库中删除字符串中的名称。我正在使用JDBC客户端来获取所有值并将它们保存在ArrayList中。但是由于某些原因,某些变量在离开while(resultset.next())
循环后会失去其值
public void getAllReqInfo(){
//ArrayList from the class "req"
reqArrayList = new ArrayList<req>();
try {
Statement stmt = con.createStatement();
String query = "SELECT * FROM req";
ResultSet resultSet = stmt.executeQuery(query);
while (resultSet.next()){
//class with all the columns from the database
req req = new req(resultSet.getInt("id"),
resultSet.getString("comments"));
reqArrayList.add(req);
//Comments != null
System.out.println(reqArrayList.get(0).getComments());
//Id != null
System.out.println(reqArrayList.get(0).getId());
} // End of while
//Comments == null
System.out.println(reqArrayList.get(0).getComments());
//Id != null
System.out.println(reqArrayList.get(0).getId());
} catch (SQLException e) {
e.printStackTrace();
}
}
“ req”类是具有getter和setter方法的自动生成的类。
我不知道为什么无论如何我都会得到帮助
答案 0 :(得分:1)
reqArrayList.get(0)
您只会在循环中和循环之后打印出第一个要求。
要倾销整个批次,请做
for (req myReq:reqArrayList){
System.out.println(myReq.getId()+" "+myReq.getComments());
}