Changeset 166

Show
Ignore:
Timestamp:
11/23/07 09:01:47 (1 year ago)
Author:
chris
Message:

Add getColumnAsArray and getColumnAsList convenience methods.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lex/trunk/src/com/qwirx/db/sql/SqlDatabase.java

    r164 r166  
    1212import java.sql.SQLException; 
    1313import java.util.ArrayList; 
     14import java.util.Iterator; 
    1415import java.util.List; 
    1516 
     
    224225            return results; 
    225226        } 
     227 
     228    /** 
     229     * Returns the first column of the results of a query as an array of Strings. 
     230     * @param query the SQL query whose results will be returned in the List 
     231     * @return a List of Strings from the first columns of the results. 
     232     * @throws DatabaseException 
     233     * @throws SQLException 
     234     */ 
     235    public String [] getColumnAsArray(String query) 
     236    throws DatabaseException, SQLException 
     237    { 
     238        List list = getColumnAsList(query); 
     239        String [] array = new String [list.size()]; 
     240         
     241        int index = 0; 
     242        for (Iterator it = list.iterator(); it.hasNext();) 
     243        { 
     244            array[index++] = (String)it.next(); 
     245        } 
     246         
     247        return array; 
     248    } 
    226249     
     250    /** 
     251     * Returns the first column of the results of a query as a List of Strings. 
     252     * @param query the SQL query whose results will be returned in the List 
     253     * @return a List of Strings from the first columns of the results. 
     254     * @throws DatabaseException 
     255     * @throws SQLException 
     256     */ 
     257    public List getColumnAsList(String query) 
     258    throws DatabaseException, SQLException 
     259    { 
     260        List results = new ArrayList(); 
     261        prepareSelect(query); 
     262        ResultSet rs = select(); 
     263         
     264        while (rs.next()) 
     265        { 
     266            results.add(rs.getString(1)); 
     267        } 
     268         
     269        finish(); 
     270        return results; 
     271    } 
     272 
    227273    /** 
    228274     * Ugly hack replacement for ResultSet.getString() because