Java JDBC Database Metadata getPrimaryKeys(Connection conn, String tableName)

Here you can find the source of getPrimaryKeys(Connection conn, String tableName)

Description

get Primary Keys

License

Open Source License

Declaration

public static List<String> getPrimaryKeys(Connection conn, String tableName) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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> getPrimaryKeys(Connection conn, String tableName) throws SQLException {
        ResultSet rs = null;/*w  ww . ja v  a  2 s . c  o  m*/

        try {
            List<String> pkNames = new ArrayList<String>();

            //get pk name
            DatabaseMetaData dbMeta = conn.getMetaData();
            rs = dbMeta.getPrimaryKeys(null, null, tableName.toUpperCase());

            while (rs.next()) {
                pkNames.add(rs.getString("COLUMN_NAME"));
            }

            return pkNames;
        } finally {
            try {
                rs.close();
            } catch (Exception e) {
            }
        }

    }
}

Related

  1. getNullableDescription(int type)
  2. getOnly1PrimaryKey(Connection conn, String tableName)
  3. getPrimaryKey(DatabaseMetaData metadata, String tableName)
  4. getPrimaryKeyColumn(DatabaseMetaData md, String TableName)
  5. getPrimaryKeyColumnNames(Connection connection, String tableName)
  6. getQualifiedTableName(DatabaseMetaData dbmd, String catalog, String schema, String table, boolean useQuotes)
  7. getSchemaPattern(final DatabaseMetaData dbData, String schemaName)
  8. getSchemas(Connection c)
  9. getSimplifiedURL(final DatabaseMetaData metadata)