Java Utililty Methods SQL Table Column

List of utility methods to do SQL Table Column

Description

The list of methods to do SQL Table Column are organized into topic(s).

Method

StringgetColunmNames(ResultSetMetaData rsmd)
get Colunm Names
StringBuilder columnNames = new StringBuilder(64);
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
    columnNames.append(rsmd.getColumnLabel(i));
return columnNames.toString();
ListgetIndexColumns(Connection connection, String schema, String table, String indexName)
Returns list of column names for the specified index
final Map<Integer, String> columns = new TreeMap<>();
ResultSet rs = null;
try {
    rs = connection.getMetaData().getIndexInfo(null, schema, table, true, false);
    while (rs.next()) {
        final String name = rs.getString("INDEX_NAME");
        if (name != null && name.equals(indexName)) {
            columns.put(rs.getInt("ORDINAL_POSITION"), rs.getString("COLUMN_NAME"));
...
StringgetIndexName(Connection conn, String table, String column)
Returns the name of the index for the specified column in the specified table.
ResultSet rs = conn.getMetaData().getIndexInfo("", "", table, false, true);
while (rs.next()) {
    String tname = rs.getString("TABLE_NAME");
    String cname = rs.getString("COLUMN_NAME");
    String iname = rs.getString("INDEX_NAME");
    if (tname.equals(table) && cname.equals(column)) {
        return iname;
return null;
double[]getRange(Connection C, String table, String column)
get Range
String query = "";
Statement S;
ResultSet R;
double[] range = new double[2];
try {
    query = "SELECT MIN(" + column + "), MAX(" + column + ") FROM " + table + ";";
    S = C.createStatement();
    R = S.executeQuery(query);
...
booleanhasColumn(final ResultSet row, final String columnName)
Test if a column name exists within the given result set.
final ResultSetMetaData metaData = row.getMetaData();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
    final String name = metaData.getColumnName(i);
    if (columnName.equals(name))
        return true;
return false;
booleanhasColumn(ResultSet rs, String columnName)
has Column
ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
for (int x = 1; x <= columns; x++) {
    if (columnName.equals(rsmd.getColumnName(x))) {
        return true;
return false;
...
booleanhasColumn(ResultSet rs, String columnName)
Checks for column.
ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
for (int x = 1; x <= columns; x++) {
    if (columnName.equalsIgnoreCase(rsmd.getColumnName(x))) {
        return true;
return false;
...
booleanhasColumn(String columnName, ResultSet resultSet)
has Column
ResultSetMetaData meta = null;
try {
    meta = resultSet.getMetaData();
    int columnCount = meta.getColumnCount();
    for (int i = 1; i < columnCount + 1; i++) {
        if (meta.getColumnName(i).equals(columnName)) {
            return true;
} catch (SQLException e) {
return false;
booleanhasColumn(String field, ResultSet rs)
Tests if a column exists in the result set.
ResultSetMetaData meta = rs.getMetaData();
int numCol = meta.getColumnCount();
for (int i = 1; i < numCol + 1; i++) {
    if (meta.getColumnLabel(i).equals(field)) {
        return true;
return false;
...
booleanhasColumns(Connection conn, String tableName, Collection colNames)
has Columns
try (java.sql.ResultSet res = conn.getMetaData().getColumns(null, null, tableName, null)) {
    while (res.next()) {
        String colName = res.getString("COLUMN_NAME");
        colNames.remove(colName.toLowerCase());
    return colNames.isEmpty();