Java SQL ResultSet Dump dumpResultSet(ResultSet resultSet, String tablename, String[] columns)

Here you can find the source of dumpResultSet(ResultSet resultSet, String tablename, String[] columns)

Description

Dump result set.

License

Apache License

Parameter

Parameter Description
resultSet the result set
tablename the tablename
columns the columns

Exception

Parameter Description
SQLException the sQL exception

Return

the list

Declaration

public static List<List<Object>> dumpResultSet(ResultSet resultSet, String tablename, String[] columns)
        throws SQLException 

Method Source Code


//package com.java2s;
/*//from w  ww .  ja va 2  s.  c  o  m
 * DumpUtil.java
 *
 * 15.09.2009
 *
 * Copyright 2009 Michael Lieshoff
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.sql.*;
import java.util.*;

public class Main {
    /**
     * Dump result set.
     *
     * @param resultSet the result set
     * @param tablename the tablename
     * @param columns the columns
     * @return the list
     * @throws SQLException the sQL exception
     */
    public static List<List<Object>> dumpResultSet(ResultSet resultSet, String tablename, String[] columns)
            throws SQLException {
        List<List<Object>> l = new ArrayList<List<Object>>();
        List<Object> t = new ArrayList<Object>();
        t.add(tablename);
        l.add(t);
        Map<String, Boolean> cs = new Hashtable<String, Boolean>();
        if (columns != null) {
            for (int i = 0, n = columns.length; i < n; i++) {
                cs.put(columns[i].toLowerCase(), true);
            }
        }
        ResultSetMetaData rsmd = resultSet.getMetaData();
        List<Object> h = new ArrayList<Object>();
        for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
            int z = i + 1;
            String name = rsmd.getColumnName(z);
            boolean add = false;
            if (cs.size() > 0) {
                if (cs.get(name.toLowerCase()) != null) {
                    add = true;
                }
            } else {
                add = true;
            }
            if (add) {
                h.add(name);
            }
        }
        l.add(h);
        while (resultSet.next()) {
            List<Object> d = new ArrayList<Object>();
            for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
                int z = i + 1;
                Object o = resultSet.getObject(z);
                String name = rsmd.getColumnName(z);
                boolean add = false;
                if (cs.size() > 0) {
                    if (cs.get(name.toLowerCase()) != null) {
                        add = true;
                    }
                } else {
                    add = true;
                }
                if (add) {
                    d.add(o);
                }
            }
            l.add(d);
        }
        resultSet.close();
        return l;
    }
}

Related

  1. dump(ResultSet rs)
  2. dump(ResultSet rset)
  3. dump(ResultSet set)
  4. dumpResultSet(ResultSet dbResult)
  5. dumpResultSet(ResultSet rs)
  6. dumpResultSet(ResultSet rs)
  7. dumpResultSetMetaData(ResultSet dbResult)
  8. dumpRs(ResultSet rs, PrintWriter out, String delimiter, boolean printTrailingDelimiter)