Reputation: 1434
I have SQL query which results in multiple columns. I want to execute this query and get the results into my ArrayList<>
instead of the ResultSet
.
My class for the column definitions is
public class Record{
private String FileName;
private String FileID;
private String Loan;
private String Page;
}
Query is :
String query = "SELECT FileName, FileID, loanNumnber, PageNumber FROM table";
ResultSet rs = stmt.executeQuery(query);
I want the results of the query in recordData object.
ArrayList<Record> recordData = new ArrayList<Record>;
Please suggest how the arraylist can be populated directly with correct mapping.
Upvotes: 3
Views: 2310
Reputation: 247
Add parenthesis "()" to this line in your code so that it looks like this:
ArrayList<Record> recordData = new ArrayList<Record>();
Upvotes: 0
Reputation: 745
Use following code snippet if you want to implement by yourself. It will convert the result set to Record
objects and add it to the 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();
Otherwise, if you want to use any third party frameworks then there are lot of options such as Hibernate, iBatis etc.
Upvotes: 3
Reputation: 919
You should use OR mapper like Hibernate. Thanks POJO classes you can do whan you want. Some readings:
http://www.tutorialspoint.com/hibernate/hibernate_examples.htm
http://www.mkyong.com/tutorials/hibernate-tutorials/
Upvotes: 0
Reputation: 1
You can use the mybatis. Please use the ORM.
https://en.wikipedia.org/wiki/Object-relational_mapping
Upvotes: 0