Java SQL Table Column isColumnNullable(Connection conn, String table, String column)

Here you can find the source of isColumnNullable(Connection conn, String table, String column)

Description

Determines whether or not the specified column accepts null values.

License

Open Source License

Return

true if the column accepts null values, false if it does not (or its nullability is unknown)

Declaration

public static boolean isColumnNullable(Connection conn, String table, String column) throws SQLException 

Method Source Code

//package com.java2s;

import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    /**/*from   w ww.  ja va2s.co m*/
     * Determines whether or not the specified column accepts null values.
     *
     * @return true if the column accepts null values, false if it does not (or its nullability is
     * unknown)
     */
    public static boolean isColumnNullable(Connection conn, String table, String column) throws SQLException {
        ResultSet rs = getColumnMetaData(conn, table, column);
        try {
            return rs.getString("IS_NULLABLE").equals("YES");
        } finally {
            rs.close();
        }
    }

    /**
     * Helper function for {@link #getColumnType}, etc.
     */
    protected static ResultSet getColumnMetaData(Connection conn, String table, String column) throws SQLException {
        ResultSet rs = conn.getMetaData().getColumns("", "", table, column);
        while (rs.next()) {
            String tname = rs.getString("TABLE_NAME");
            String cname = rs.getString("COLUMN_NAME");
            if (tname.equals(table) && cname.equals(column)) {
                return rs;
            }
        }
        throw new SQLException("Table or Column not defined. [table=" + table + ", col=" + column + "].");
    }

    /**
     * Closes the supplied JDBC statement and gracefully handles being passed null (by doing
     * nothing).
     */
    public static void close(Statement stmt) throws SQLException {
        if (stmt != null) {
            stmt.close();
        }
    }

    /**
     * Closes the supplied JDBC connection and gracefully handles being passed null (by doing
     * nothing).
     */
    public static void close(Connection conn) throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }
}

Related

  1. hasColumn(String field, ResultSet rs)
  2. hasColumns(Connection conn, String tableName, Collection colNames)
  3. hasTableAndColumns(Connection conn, String tableName, String... colNames)
  4. indent_DisplayBanner(PrintWriter out, ResultSetMetaData rsmd, int indentLevel, int[] displayColumns, int[] displayColumnWidths)
  5. isColumnNullable(Connection conn, String table, String column)
  6. isNull(ResultSet resultSet, String columnName)
  7. lookupColumnIndex(final ResultSetMetaData resultSetMetaData, final String columnName)
  8. lookupColumnIndex(ResultSetMetaData resultSetMetaData, String name)
  9. lookupColumnName(ResultSetMetaData resultSetMetaData, int columnIndex)