I have a SQL query that returns a number of results (unfortunately, the number of results will vary). Currently, I am storing the results into an arrayList like so:
ArrayList allTerms = new ArrayList();
try {
String selStmt = "SELECT .... ";
stmt = conn.prepareStatement(selStmt);
result = stmt.executeQuery();
Object result_data;
while (result.next()) {
term = (((result_data = result.getObject("internal_code"))==null || result.wasNull())?" ":result_data.toString());
allTerms.add(term);
desc = (((result_data = result.getObject("external_representation"))==null || result.wasNull())?" ":result_data.toString());
allTerms.add(desc);
sorter = (((result_data = result.getObject("sorter"))==null || result.wasNull())?" ":result_data.toString());
allTerms.add(sorter);
sDate = (开发者_运维技巧((result_data = result.getObject("sDate"))==null || result.wasNull())?" ":result_data.toString());
allTerms.add(sDate);
}
System.out.println(allTerms);
Okay, so when I run this, the system prints:
[2011SP, Spring 2011, 1, 11-15-2010, 2011SU, Summer 2011, 1, 01-15-2011, 2011FL, Fall 2011, 1, 04-01-2011, 2010Q2, CE Qtr 2 2010 Dec - Feb, 2, 08-01-2010, 2011Q3, CE Qtr 3 2011 Mar - May, 2, 11-01-2010, 2011Q4, CE Qtr 4 2011 Jun - Aug, 2, 02-01-2011, 2011Q1, CE Qtr 1 2011 Sep-Nov, 2, 05-01-2011]
I dont know if this is the correct way of doing it or not, but it is working so far, so what I want to do now is use my allTerms arrayList to transfer these results to parts of a HTML page. so for ex:
<table class="t1">
<tr>
<td><!--Here I would want to show all rows from the allTerms arrayList with a "sorter" of 1--></td>
</tr>
</table>
<table class="t2">
<tr>
<td><!--Here I would want to show all rows from the allTerms arrayList with a "sorter" of 1 and a term of ....SU (where '....' is the year) --></td>
</tr>
</table>
It's a lot easier if you just define a short value holder class, like:
public class Row { // give it a better name ;)
public String internalCode;
public String externalRepresentation;
public String sorter;
public String sDate;
}
then, define a collection that holds these row values:
List<Row> rows = new ArrayList<Row>();
and add the rows to the list:
while (result.next()) {
Row row = new Row();
row.internalCode = (((result_data = result.getObject("internal_code"))==null || result.wasNull())?" ":result_data.toString());
row.externalRepresentation = (((result_data = result.getObject("external_representation"))==null || result.wasNull())?" ":result_data.toString());
row.sorter = (((result_data = result.getObject("sorter"))==null || result.wasNull())?" ":result_data.toString());
row.sDate = (((result_data = result.getObject("sDate"))==null || result.wasNull())?" ":result_data.toString());
}
Now you can filter on the collection like this:
for (Row row:rows) {
if (!row.sorter.equals("1"))
continue;
// do what has to be done with records where sorter = "1"
}
精彩评论