Java SQL Table dumpTable(Connection connection, String tablename)

Here you can find the source of dumpTable(Connection connection, String tablename)

Description

Dump table.

License

Apache License

Parameter

Parameter Description
connection the connection
tablename the tablename

Exception

Parameter Description
SQLException the sQL exception

Return

the list

Declaration

public static List<List<Object>> dumpTable(Connection connection, String tablename) throws SQLException 

Method Source Code


//package com.java2s;
/*//from  w w w.  j  av  a 2 s . com
 * 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 {
    private final static String SQL_SELECT_ALL = "select * from %1";

    /**
     * Dump table.
     *
     * @param connection the connection
     * @param tablename the tablename
     * @return the list
     * @throws SQLException the sQL exception
     */
    public static List<List<Object>> dumpTable(Connection connection, String tablename) throws SQLException {
        return dumpTable(connection, tablename, null);
    }

    /**
     * Dump table.
     *
     * @param connection the connection
     * @param tablename the tablename
     * @param columns the columns
     * @return the list
     * @throws SQLException the sQL exception
     */
    public static List<List<Object>> dumpTable(Connection connection, String tablename, String[] columns)
            throws SQLException {
        return dumpTable(connection, tablename, null, columns);
    }

    /**
     * Dump table.
     *
     * @param connection the connection
     * @param tablename the tablename
     * @param where the where
     * @param columns the columns
     * @return the list
     * @throws SQLException the sQL exception
     */
    public static List<List<Object>> dumpTable(Connection connection, String tablename, String where,
            String[] columns) throws SQLException {
        String q = SQL_SELECT_ALL.replace("%1", tablename);
        if (where != null && where.length() > 0) {
            q += " where " + where;
        }
        Statement s = connection.createStatement();
        ResultSet rs = s.executeQuery(q);
        s.close();
        return dumpResultSet(rs, tablename, columns);
    }

    /**
     * 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. createTable(Statement statement, String spaceName, String minSeeders, String capacity, String replicationCount)
  2. createTable(String createTblSql, Connection conn)
  3. createTableAndInsertData(Connection c)
  4. dropTable(String tableName, Connection con)
  5. dumpTable(Connection conn, String TableName, PrintWriter out)
  6. dumpTableToFile(Connection con, String table, String fileName)
  7. exportTableData(String tableName, Connection con)
  8. getAllTableNames(Connection connection)
  9. getAllTables(Connection conn)