001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.sql;
022    
023    import java.sql.ResultSet;
024    import java.sql.ResultSetMetaData;
025    import java.sql.SQLException;
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /** A Helper Class to make work with JDBC less painful in some situations.
030     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
031     */
032    public class SQLHelper {
033    
034        /** The Type information for a two-dimensional Object Array. */
035        private static final Object[][] OBJECT_ARRAY_ARRAY_INSTANCE = new Object[0][0];
036    
037        /** Get a ResultSet's Column Names.
038         * The names are physical.
039         * For names for display @see #getColumnLabels(ResultSet).
040         * @param rs ResultSet
041         * @return column names
042         * @throws SQLException on SQL problems
043         */
044        public static String[] getColumnNames(final ResultSet rs) throws SQLException {
045            final ResultSetMetaData md = rs.getMetaData();
046            final int columnCount = md.getColumnCount();
047            final String[] columnNames = new String[columnCount];
048            for (int i = 0; i < columnCount; i++) {
049                columnNames[i] = md.getColumnName(i + 1);
050            }
051            return columnNames;
052        }
053    
054        /** Get a ResultSet's Column Labels.
055         * The names are for display.
056         * For physical names @see #getColumnNames(ResultSet).
057         * @param rs ResultSet
058         * @return column labels
059         * @throws SQLException on SQL problems
060         */
061        public static String[] getColumnLabels(final ResultSet rs) throws SQLException {
062            final ResultSetMetaData md = rs.getMetaData();
063            final int columnCount = md.getColumnCount();
064            final String[] columnNames = new String[columnCount];
065            for (int i = 0; i < columnCount; i++) {
066                columnNames[i] = md.getColumnLabel(i + 1);
067            }
068            return columnNames;
069        }
070    
071        /** Get a ResultSet's result.
072         * @param rs ResultSet
073         * @return two-dimensional Array containing the results
074         * @throws SQLException on SQL problems
075         */
076        public static Object[][] getData(final ResultSet rs) throws SQLException {
077            final List<Object[]> resultList = new ArrayList<Object[]>();
078            final ResultSetMetaData md = rs.getMetaData();
079            final int columnCount = md.getColumnCount();
080            while (rs.next()) {
081                final Object[] row = new Object[columnCount];
082                for (int i = 0; i < columnCount; i++) {
083                    row[i] = rs.getObject(i + 1);
084                }
085                resultList.add(row);
086            }
087            return resultList.toArray(OBJECT_ARRAY_ARRAY_INSTANCE);
088        }
089    
090        /** Get a ResultSet's size.
091         * Warning: The ResultSet's Cursor is moved by invoking this method!
092         * @param rs ResultSet
093         * @return the number of rows in this ResultSet
094         * @throws SQLException on SQL problems
095         */
096        public static int getRowCount(final ResultSet rs) throws SQLException {
097            return rs.last() ? rs.getRow() : 0;
098        }
099    
100    } // class SQLHelper