Java JDBC Database Metadata getTableMetadata(Connection connection, String tableName)

Here you can find the source of getTableMetadata(Connection connection, String tableName)

Description

get Table Metadata

License

Apache License

Declaration

public static Map<String, Map<String, String>> getTableMetadata(Connection connection, String tableName)
            throws Exception 

Method Source Code


//package com.java2s;
/* //from   w  w w. j av a2  s . co  m
 * Copyright 2012-2017 qifu of copyright Chen Xin Nien
 * 
 * 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.
 * 
 * -----------------------------------------------------------------------
 * 
 * author:    Chen Xin Nien
 * contact: chen.xin.nien@gmail.com
 * 
 */

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

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static Map<String, Map<String, String>> getTableMetadata(Connection connection, String tableName)
            throws Exception {
        Map<String, Map<String, String>> tableMetadatas = new HashMap<String, Map<String, String>>();
        DatabaseMetaData metadata = connection.getMetaData();
        ResultSet resultSet = metadata.getColumns(null, null, tableName, null);
        while (resultSet.next()) {
            Map<String, String> metaDataMap = new HashMap<String, String>();
            String name = resultSet.getString("COLUMN_NAME");
            String type = resultSet.getString("TYPE_NAME");
            int size = resultSet.getInt("COLUMN_SIZE");
            metaDataMap.put("COLUMN_NAME", name);
            metaDataMap.put("TYPE_NAME", type);
            metaDataMap.put("COLUMN_SIZE", Integer.toString(size));
            tableMetadatas.put(name, metaDataMap);
        }
        return tableMetadatas;
    }
}

Related

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