Answer the question
In order to leave comments, you need to log in
How to create a list of objects from a Result Set?
Question about the cycle. Let's say in the set I have 4 columns or 4 rows.
1.After while(rs.next()) in this code-> setters will be called 4 times and one object will be created?
2. Let's say I need to create a list of objects, the Result Set structure with data is not fully understood, if anyone can give a code example, I will be grateful.
3.next() returns true if there is a next column, how does the logic in this code go in the loop?
Whether the necessary object will be created after the first cycle?
Company company = beanFactory.getBeanInstance("company",classForMapping);
while(rs.next()){ // 4 number of columns
company.setId(rs.getLong("COMPANY_ID"));
company.setCompName(rs.getNString("COMPANY_NAME"));
company.setPassword(rs.getNString("COMPANY_PASSWORD"));
company.setEmail(rs.getNString("COMPANY_EMAIL"));
}
Answer the question
In order to leave comments, you need to log in
something like this
List<Company> list = new ArrayList();
while(rs.next()){ // 4 number of columns
Company company = beanFactory.getBeanInstance("company",classForMapping);
company.setId(rs.getLong("COMPANY_ID"));
company.setCompName(rs.getNString("COMPANY_NAME"));
company.setPassword(rs.getNString("COMPANY_PASSWORD"));
company.setEmail(rs.getNString("COMPANY_EMAIL"));
list.add(company);
}
The setters will be called as long as there are rows returned from the database. For example, the database returns 2 rows corresponding to your selection, therefore, if there are 4 columns, then the setters will be called 8 times.
next() returns true if there is another line behind the cursor, i.e. if you explain earlier with the above example:
-> ( this is a kind of cursor
row1
row2
next() will return true two cycles in a row. And here it will already be false, because there is no row behind the cursor:
row1
row2
-> Got it
? With each call to next( ) the cursor moves forward, the setters return the data of the corresponding line to which the cursor points
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question