England Forum - UK Forum
 

Go Back   England Forum - UK Forum > Computers and Technology > Developers Forum > Java Forum

 

 


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-08-2008
Peon
 
Join Date: May 2008
Posts: 35
Default Java JDBC resultset to List?

I am trying to put my resultset into a list. Problem is that i want my list to be of type Integer, and the getInteger method takes as a parameter a system property, which i have no idea what this is. This is my code.
String SELECT_TOP_TIMES =
"select TOP 3 r.result " +
"from tblResults as r " +
"where r.Event_ID = " +
"(select e.ID " +
"from tblEvent as e " +
"where e.Event_Name = '100M Run') " +
"and r.Round_ID = " +
"(select ro.ID " +
"from tblRound as ro " +
"where ro.Round_Number = 'Round_1') " +
"order by r.result";


PreparedStatement ps = null;
ResultSet rs = null;
List<Integer> topTimes = new ArrayList<Integer>();

try
{
con = DatabaseUtils.connect(DRIVER, URL);
ps = con.prepareStatement(SELECT_TOP_TIMES);
rs = ps.executeQuery();
while (rs.next())
{
int result = rs.getInteger(1);
topTimes.add(result);
}

System.out.println(topTimes);

}

Any help would be appreciated in getting this to create a list of Integer value.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links

  #2 (permalink)  
Old 11-08-2008
Peon
 
Join Date: Nov 2008
Posts: 2
Default

You can either write a data transfer object that encapsulates all those columns, OR you can save them in a List of Maps, where each Map represents one row and the keys are the column names. Here's a method that will do it for you:

/**
* Helper method that converts a ResultSet into a list of maps, one per row
* @param query ResultSet
* @return list of maps, one per row, with column name as the key
* @throws SQLException if the connection fails
*/
public static final List toList(ResultSet rs) throws SQLException
{
List wantedColumnNames = ResultSetMapper.getColumnNames(rs);

return ResultSetMapper.toList(rs, wantedColumnNames);
}
/**
* Helper method that maps a ResultSet into a list of maps, one per row
* @param query ResultSet
* @param list of columns names to include in the result map
* @return list of maps, one per column row, with column names as keys
* @throws SQLException if the connection fails
*/
public static final List toList(ResultSet rs, List wantedColumnNames) throws SQLException
{
List rows = new ArrayList();

int numWantedColumns = wantedColumnNames.size();
while (rs.next())
{
Map row = new TreeMap();

for (int i = 0; i < numWantedColumns; ++i)
{
String columnName = (String)wantedColumnNames.get(i);
Object value = rs.getObject(columnName);
row.put(columnName, value);
}

rows.add(row);
}

return rows;
}




/**
* Return all column names as a list of strings
* @param database query result set
* @return list of column name strings
* @throws SQLException if the query fails
*/
public static final List getColumnNames(ResultSet rs) throws SQLException
{
List columnNames = new ArrayList();

ResultSetMetaData meta = rs.getMetaData();

int numColumns = meta.getColumnCount();
for (int i = 1; i <= numColumns; ++i)
{
columnNames.add(meta.getColumnName(i));
}

return columnNames;
}


Hope this will help
Cheers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:03 AM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC7
Sedo - Buy and Sell Domain Names and Websites project info: englanddebate.co.uk Statistics for project englanddebate.co.uk etracker® web controlling instead of log file analysis

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206