Java SQL Table Column printTableColumn(Connection conn, String sql)

Here you can find the source of printTableColumn(Connection conn, String sql)

Description

print Table Column

License

Open Source License

Declaration

public static void printTableColumn(Connection conn, String sql) throws Exception 

Method Source Code


//package com.java2s;
import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    public static void printTableColumn(Connection conn, String sql) throws Exception {
        Statement stmt = null;// w w  w. j a va2 s . co m
        ResultSet rs = null;

        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            ResultSetMetaData metadata = rs.getMetaData();
            int columns = metadata.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                System.out.print(metadata.getColumnName(i) + "/" + metadata.getColumnTypeName(i) + "  ");
            }
            System.out.println("\n");
        } catch (Exception e) {
            throw e;
        } finally {
            close(rs, stmt);
        }
    }

    public static void executeQuery(Connection conn, String sql) throws Exception {

        System.out.println("Query SQL: " + sql);

        Statement stmt = null;
        ResultSet rs = null;

        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            ResultSetMetaData metadata = rs.getMetaData();
            int columns = metadata.getColumnCount();
            for (int row = 1; rs.next(); row++) {
                System.out.print(row + ": ");
                for (int i = 0; i < columns; i++) {
                    if (i > 0) {
                        System.out.print(", ");
                    }
                    System.out.print(rs.getString(i + 1));
                }
                System.out.println();
            }
        } catch (Exception e) {
            throw e;
        } finally {
            close(rs, stmt);
        }

        System.out.println();

    }

    public static void close(Connection conn) {

        if (null != conn) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {

            }
        }
    }

    public static void close(Statement stmt) {

        if (null != stmt) {
            try {
                stmt.close();
                stmt = null;
            } catch (SQLException e) {

            }
        }
    }

    public static void close(ResultSet rs, Statement stmt) {

        if (null != rs) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
            }
        }

        if (null != stmt) {
            try {
                stmt.close();
                stmt = null;
            } catch (SQLException e) {
            }
        }
    }
}

Related

  1. isColumnNullable(Connection conn, String table, String column)
  2. isNull(ResultSet resultSet, String columnName)
  3. lookupColumnIndex(final ResultSetMetaData resultSetMetaData, final String columnName)
  4. lookupColumnIndex(ResultSetMetaData resultSetMetaData, String name)
  5. lookupColumnName(ResultSetMetaData resultSetMetaData, int columnIndex)
  6. readMultiDouble(ResultSet resultSet, String columnName)
  7. readString(final ResultSet resultSet, final String columnName)
  8. readStringFromBlob(ResultSet resultSet, String columnLabel)
  9. resultSetAsCount(ResultSet rs, String countColumn)