1
public List<String> getColumnDefaultValues(Connection conn,String tableName) throws SQLException{

    List<String> colDefVal = new ArrayList<>();

    ResultSet rsColumns = null;
    DatabaseMetaData meta = conn.getMetaData();
    rsColumns = meta.getColumns(null, null, tableName, null);

    while (rsColumns.next()) {
        colDefVal.add(rsColumns.getString("COLUMN_DEF"));
    }

    return colDefVal;
}

I'm connecting to MySQL using JDBC. The following function returns a list containing the default values for all columns in a specified table. It's returning the correct default values e.g. "abc". But it returns null for these both cases: in case a default value is set to NULL or in case a default was not set on a column at all.
How can I distinguish between a column having a default NULL constraint and a column with no default constraint?

yogur
  • 810
  • 10
  • 19
  • Setting a column's default value to `NULL` isn't really meaningful anyways; [what are you actually trying to do?](http://xyproblem.info/) – dimo414 Feb 01 '16 at 21:03
  • I'm building an application similar to PhpMyAdmin using JDBC. That's why I need to get data about columns of a table. – yogur Feb 01 '16 at 21:13

1 Answers1

0

JDBC can't give you any information which is not available in the database itself.

According to this answer Mysql does not differentiate between these column definitions:

columnname type
columnname type NULL
columnname type DEFAULT NULL
columnname type NULL DEFAULT NULL
Community
  • 1
  • 1
wero
  • 32,544
  • 3
  • 59
  • 84