Java JDBC Database Metadata getPrimaryKeyColumn(DatabaseMetaData md, String TableName)

Here you can find the source of getPrimaryKeyColumn(DatabaseMetaData md, String TableName)

Description

get Primary Key Column

License

Apache License

Declaration

private static String getPrimaryKeyColumn(DatabaseMetaData md, String TableName) throws SQLException 

Method Source Code

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

import java.sql.*;

public class Main {
    private static String getPrimaryKeyColumn(DatabaseMetaData md, String TableName) throws SQLException {
        String PK_Col = null;/*from   w  w  w  .  j  a va 2s .  c o  m*/
        ResultSet rs2 = md.getPrimaryKeys(null, null, TableName);
        while (rs2.next()) {
            String[] items = getResultSetStrings(rs2);
            if (PK_Col == null) {
                PK_Col = rs2.getString(4);
            } else {
                PK_Col = null;
                break;
            }
        }
        rs2.close();
        return PK_Col;
    }

    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. getForeignKeyDeferrability(int code)
  2. getJDBCMajorVersion(Connection conn)
  3. getNullableDescription(int type)
  4. getOnly1PrimaryKey(Connection conn, String tableName)
  5. getPrimaryKey(DatabaseMetaData metadata, String tableName)
  6. getPrimaryKeyColumnNames(Connection connection, String tableName)
  7. getPrimaryKeys(Connection conn, String tableName)
  8. getQualifiedTableName(DatabaseMetaData dbmd, String catalog, String schema, String table, boolean useQuotes)
  9. getSchemaPattern(final DatabaseMetaData dbData, String schemaName)