Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top