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

StringadjustIdentifierCase(String identifier, Connection conn)
Adjusts an SQL identifier to correct case according to the database convention.
DatabaseMetaData md = conn.getMetaData();
if (!md.storesMixedCaseIdentifiers() && identifier != null) {
    if (md.storesUpperCaseIdentifiers()) {
        return identifier.toUpperCase();
    if (md.storesLowerCaseIdentifiers()) {
        return identifier.toLowerCase();
return identifier;
voidcreateHsqlPlSchemaIfNecessary(Connection con)
Creates a schema called "pl" in the given HSQLDB connection.
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getSchemas();
boolean foundPlSchema = false;
while (rs.next()) {
    if ("pl".equalsIgnoreCase(rs.getString("TABLE_SCHEM"))) {
        foundPlSchema = true;
rs.close();
if (!foundPlSchema) {
    Statement stmt = con.createStatement();
    stmt.executeUpdate("CREATE SCHEMA pl AUTHORIZATION DBA");
    stmt.close();
SetgetAllTables(Connection connection)
get All Tables
Set<String> tables = new HashSet<String>();
DatabaseMetaData db = connection.getMetaData();
ResultSet rs = db.getTables(null, null, "%", new String[] { "TABLE" });
while (rs.next()) {
    tables.add(rs.getString(3));
return tables;
ResultSetgetAllTables(Connection connection)
get All Tables
DatabaseMetaData meta = connection.getMetaData();
ResultSet tableNames = meta.getTables(null, null, TABLE_NAME_PATTERN_ALL, null);
return tableNames;
ListgetCatalogs(Connection c)
get Catalogs
DatabaseMetaData dmd = c.getMetaData();
ResultSet rs = null;
try {
    rs = dmd.getCatalogs();
    List l = new LinkedList();
    while (rs.next()) {
        l.add(rs.getString(1));
    return l;
} finally {
    if (rs != null)
        rs.close();
ObjectgetColumnDefaultValue(DatabaseMetaData metaData, String tableName, String columnName)
Returns the default value of a column (as per its SQL definition).
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
    if (!rs.next()) {
        throw new IllegalStateException(
                "Did not find meta data for column '" + columnName + "' while checking its default value");
    return rs.getObject("COLUMN_DEF");
ArrayListgetColumnNames(Connection connection, String tableName)
get Column Names
ArrayList<String> columnNames = new ArrayList<>();
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet resultSet = databaseMetaData.getColumns(null, null, tableName, null);
while (resultSet.next()) {
    columnNames.add(resultSet.getString("COLUMN_NAME"));
return columnNames;
ListgetColumnNames(String tablename, String column, Connection conn)
get Column Names
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getColumns(null, null, tablename, column);
List columns = null;
while (rs.next()) {
    String columnName = rs.getString("COLUMN_NAME");
    if (columns == null) {
        columns = new ArrayList();
    columns.add(columnName);
return columns;
ResultSetgetColumns(Connection connection, String name)
get Columns
DatabaseMetaData meta = connection.getMetaData();
if (name == null) {
    meta.getColumns(null, null, name, null);
ResultSet columns = meta.getColumns(null, null, name, null);
if (columns.next()) {
    return meta.getColumns(null, null, name, null);
} else {
...
intgetColumnSize(Connection con, String tableName, String columnName)
get Column Size
DatabaseMetaData metaData = con.getMetaData();
if (metaData.storesLowerCaseIdentifiers()) {
    tableName = tableName.toLowerCase();
    columnName = columnName.toLowerCase();
} else if (metaData.storesUpperCaseIdentifiers()) {
    tableName = tableName.toUpperCase();
    columnName = columnName.toUpperCase();
try (Statement statement = con.createStatement()) {
    ResultSet resultSet = statement.executeQuery("SELECT * from " + tableName);
    ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    int columnCount = resultSetMetaData.getColumnCount();
    for (int i = 1; i <= columnCount; i++) {
        if (resultSetMetaData.getColumnName(i).equals(columnName)) {
            return resultSetMetaData.getColumnDisplaySize(i);
return -1;