I am doing this in order to read the result set into an array of hashmaps:
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
ArrayList list = new ArrayList(50);
while (rs.next()){
HashMap row = new HashMap(columns);
for(int i=1; i<=columns; ++i){
row.put(md.getColumnName(i),rs.getString(i));
}
list.add(row);
}
but when I do a query that is like "SELECT id AS contact_id from contacts" I am obviously getting id=>#### instead of the desired contact_id. What is the best way to go about this? This topic is super hard to search for. :P Thanks!
Got it. The answer was to search for "get column alias":
http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSetMetaData.html
Changed to:
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
ArrayList list = new ArrayList(50);
while (rs.next()){
HashMap row = new HashMap(columns);
for(int i=1; i<=columns; ++i){
row.put(md.getColumnLabel(i),rs.getString(i));
}
list.add(row);
}
works great.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.