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

booleancolumnExists(Connection con, String tableName, String columnName)
Tests if a column exists in the database
if (tableName == null) {
    throw new NullPointerException("tableName cannot be null");
if (columnName == null) {
    throw new NullPointerException("columnName cannot be null");
ResultSet res = con.getMetaData().getColumns(null, null, tableName, columnName);
while (res.next()) {
...
booleancolumnExists(Connection connection, String table, String column)
column Exists
ResultSet columns = null;
boolean exists;
try {
    columns = connection.getMetaData().getColumns(null, null, table, column);
    exists = columns.next();
} finally {
    if (columns != null) {
        columns.close();
...
SetcolumnLabels(final ResultSet results)
column Labels
final ResultSetMetaData rsmd = results.getMetaData();
final int columnCount = rsmd.getColumnCount();
final Set<String> columnLabels = new HashSet<String>(columnCount);
for (int i = 1; i <= columnCount; i++) {
    columnLabels.add(rsmd.getColumnLabel(i));
return columnLabels;
booleancontainsColumn(ResultSet rs, String columnName)
contains Column
try {
    rs.findColumn(columnName);
    return true;
} catch (SQLException e) {
    return false;
StringextractColumnTypeName(ResultSetMetaData resultSetMetaData, int index)
extract Column Type Name
return resultSetMetaData.getColumnTypeName(index);
intgenerateKey(Statement statement, String TableName, String KeyColumn)
generate Key
String sql = "SELECT MAX(" + KeyColumn + ") FROM  " + TableName;
return getQueryInt(statement, sql);
ResultSetgetAllFromSensorDb(final Connection jdbcConnection, final String tableName, final String orderByColumn)
get All From Sensor Db
Logger.getAnonymousLogger().log(Level.INFO, "Querying sensor DB to stream all data.");
Statement stmt = jdbcConnection.createStatement();
stmt.setFetchSize(10000);
stmt.setQueryTimeout(0);
String sql = MessageFormat.format("SELECT * FROM {0} {1} ORDER BY {2}", tableName, PREDICATE,
        orderByColumn);
return stmt.executeQuery(sql);
StringgetColumn(ResultSet result, int col)
Get a string from a result set, trimming trailing blanks.
String s = result.getString(col);
if (s != null) {
    s = s.trim();
return s;
intgetColumnDisplaySize(int col, ResultSetMetaData rsmd)
get Column Display Size
int columnSize = rsmd.getColumnDisplaySize(col);
return columnSize;
intgetColumnIndexFromColumnName(ResultSetMetaData metaData, String columnName)
Returns the column index for the guven column name.
for (int i = 1; i <= metaData.getColumnCount(); i++) {
    String tmpName = metaData.getColumnLabel(i);
    if (tmpName.equalsIgnoreCase(columnName)) {
        return i;
return -1;