Java SQL Table dumpTable(Connection conn, String TableName, PrintWriter out)

Here you can find the source of dumpTable(Connection conn, String TableName, PrintWriter out)

Description

dump Table

License

Apache License

Declaration

public static void dumpTable(Connection conn, String TableName, PrintWriter out) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.sql.*;

public class Main {
    public static void dumpTable(Connection conn, String TableName, PrintWriter out) throws SQLException {
        Statement st = conn.createStatement();
        String Query = "Select * from " + TableName;
        ResultSet rs = st.executeQuery(Query);
        while (rs.next()) {
            String[] items = getResultSetStrings(rs);
            for (int i = 0; i < items.length; i++) {
                String item = items[i];
                out.print(item);//from ww w. j a v a  2 s .  c  om
                if (i < items.length - 1)
                    out.print(", ");
            }
            out.println();
        }
    }

    public static void dumpTable(Connection conn, String TableName, PrintStream out) throws SQLException {
        Statement st = conn.createStatement();
        String Query = "Select * from " + TableName;
        ResultSet rs = st.executeQuery(Query);
        while (rs.next()) {
            String[] items = getResultSetStrings(rs);
            for (int i = 0; i < items.length; i++) {
                String item = items[i];
                out.print(item);
                if (i < items.length - 1)
                    out.print(", ");
            }
            out.println();
        }
    }

    public static String[] getResultSetStrings(ResultSet rs) throws SQLException {
        int nCols = getNumberColumns(rs);
        String[] ret = new String[nCols];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = rs.getString(i + 1);
        }
        return (ret);
    }

    public static int getNumberColumns(ResultSet rs) throws SQLException {
        ResultSetMetaData md = rs.getMetaData();
        int nCols = md.getColumnCount();
        return (nCols);
    }
}

Related

  1. createTable(Connection dbCon, String tableName, String fields)
  2. createTable(Statement statement, String spaceName, String minSeeders, String capacity, String replicationCount)
  3. createTable(String createTblSql, Connection conn)
  4. createTableAndInsertData(Connection c)
  5. dropTable(String tableName, Connection con)
  6. dumpTable(Connection connection, String tablename)
  7. dumpTableToFile(Connection con, String table, String fileName)
  8. exportTableData(String tableName, Connection con)
  9. getAllTableNames(Connection connection)