Java Utililty Methods JDBC Database Metadata

List of utility methods to do JDBC Database Metadata

Description

The list of methods to do JDBC Database Metadata are organized into topic(s).

Method

StringgetForeignKeyDeferrability(int code)
get Foreign Key Deferrability
switch (code) {
case DatabaseMetaData.importedKeyInitiallyDeferred:
    return "Initially deferred";
case DatabaseMetaData.importedKeyInitiallyImmediate:
    return "Initially immediate";
case DatabaseMetaData.importedKeyNotDeferrable:
    return "Not deferrable";
return "???";
intgetJDBCMajorVersion(Connection conn)
Get the JDBC version, inferring it from the driver.
try {
    conn.getClass().getMethod("setSavepoint", null);
    DatabaseMetaData meta = conn.getMetaData();
    Method method = meta.getClass().getMethod("getJDBCMajorVersion", null);
    return ((Number) method.invoke(meta, null)).intValue();
} catch (Throwable t) {
    return 2;
StringgetNullableDescription(int type)
get Nullable Description
switch (type) {
case DatabaseMetaData.typeNoNulls:
    return "No";
case DatabaseMetaData.typeNullable:
    return "Yes";
default:
    return "Unknown";
StringgetOnly1PrimaryKey(Connection conn, String tableName)
get Only Primary Key
ResultSet rs = null;
try {
    String pkName = null;
    DatabaseMetaData dbMeta = conn.getMetaData();
    rs = dbMeta.getPrimaryKeys(null, null, tableName.toUpperCase());
    if (rs.next()) {
        pkName = rs.getString("COLUMN_NAME");
    return pkName;
} finally {
    try {
        rs.close();
    } catch (Exception e) {
SetgetPrimaryKey(DatabaseMetaData metadata, String tableName)
Returns a table's primary key columns as a Set of strings.
Set<String> columns = new HashSet<String>();
ResultSet keys = metadata.getPrimaryKeys(metadata.getConnection().getCatalog(), metadata.getUserName(),
        tableName);
while (keys.next()) {
    columns.add(keys.getString(PRIMARY_PK_COL_NAME));
keys.close();
return columns;
...
StringgetPrimaryKeyColumn(DatabaseMetaData md, String TableName)
get Primary Key Column
String PK_Col = null;
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;
...
ListgetPrimaryKeyColumnNames(Connection connection, String tableName)
get Primary Key Column Names
List<String> primaryKeyColumnNames = new LinkedList<String>();
DatabaseMetaData dbm = connection.getMetaData();
ResultSet rs = dbm.getPrimaryKeys(null, null, tableName);
while (rs.next()) {
    String keyColumnName = rs.getString("COLUMN_NAME");
    primaryKeyColumnNames.add(keyColumnName);
rs.close();
...
ListgetPrimaryKeys(Connection conn, String tableName)
get Primary Keys
ResultSet rs = null;
try {
    List<String> pkNames = new ArrayList<String>();
    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) {
StringgetQualifiedTableName(DatabaseMetaData dbmd, String catalog, String schema, String table, boolean useQuotes)
Return a fully qualified table name.
boolean catalogSupported = dbmd.supportsCatalogsInTableDefinitions();
boolean schemaSupported = dbmd.supportsSchemasInTableDefinitions();
String quote = dbmd.getIdentifierQuoteString();
String separator = dbmd.getCatalogSeparator();
if (separator == null || separator.trim().length() == 0) {
    separator = ".";
StringBuilder sb = new StringBuilder();
...
StringgetSchemaPattern(final DatabaseMetaData dbData, String schemaName)
Lookup schema name according do database metadata see JIRA-28526 this method needs to be coherent with #convertToSchemaTableName(String,java.sql.DatabaseMetaData) and ModelEntity#getTableName(org.ofbiz.core.entity.config.DatasourceInfo)
if (dbData.supportsSchemasInTableDefinitions()) {
    if (schemaName != null && schemaName.length() > 0) {
        return schemaName;
    } else if ("Oracle".equalsIgnoreCase(dbData.getDatabaseProductName())) {
        return dbData.getUserName();
return null;
...