Java JDBC Database Metadata getTableColumns(Connection connection, String selectedTable)

Here you can find the source of getTableColumns(Connection connection, String selectedTable)

Description

get Table Columns

License

Open Source License

Declaration

public static List<String> getTableColumns(Connection connection, String selectedTable) throws SQLException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 ******************************************************************************/

import java.sql.Connection;
import java.sql.DatabaseMetaData;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<String> getTableColumns(Connection connection, String selectedTable) throws SQLException {

        ResultSet rs = null;/*  w w w  .j a  v a  2s  .c  om*/

        try {
            DatabaseMetaData meta = connection.getMetaData();

            rs = meta.getColumns(null, null, selectedTable, null);

            ArrayList<String> columnNames = new ArrayList<String>();

            while (rs.next()) {

                columnNames.add(rs.getString(4));
            }

            return columnNames;

        } finally {
            closeResultSet(rs);
            closeConnection(connection);
        }
    }

    public static void closeResultSet(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
        }
    }

    public static void closeConnection(Connection connection) {
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        } catch (SQLException e) {
        }
    }
}

Related

  1. getPrimaryKeys(Connection conn, String tableName)
  2. getQualifiedTableName(DatabaseMetaData dbmd, String catalog, String schema, String table, boolean useQuotes)
  3. getSchemaPattern(final DatabaseMetaData dbData, String schemaName)
  4. getSchemas(Connection c)
  5. getSimplifiedURL(final DatabaseMetaData metadata)
  6. getTableMetadata(Connection connection, String tableName)
  7. getTableNames(Connection conn)
  8. getTableNames(DatabaseMetaData dbMD)
  9. isDB2(DatabaseMetaData metadata)