我有导致多列的SQL查询.我想执行此查询并将结果放入我的ArrayList<>中.而不是ResultSet.
我的列定义课是
public class Record{
private String FileName;
private String FileID;
private String Loan;
private String Page;
}
查询是:
String query = "SELECT FileName, FileID, loanNumnber, PageNumber FROM table";
ResultSet rs = stmt.executeQuery(query);
我想要在recordData对象中查询的结果.
ArrayList<Record> recordData = new ArrayList<Record>;
请建议如何使用正确的映射直接填充arraylist.
解决方法:
如果您想自己实现,请使用以下代码段.它将结果集转换为Record对象,并将其添加到ArrayList中.
Record record;
while(rs.next()){
record = new record();
String fileName = rs.getString("FileName");
String fileID = rs.getString("FileID");
String loanNumnber = rs.getString("loanNumnber");
String pageNumber = rs.getString("PageNumber");
record.setFileName(fileName);
record.setFileID(fileID);
record.setLoan(loanNumnber);
record.setPage(pageNumber);
recordData.add(record)
}
rs.close();
否则,如果您想使用任何第三方框架,则有很多选择,例如Hibernate,iBatis等.