col.setName(columnName); col.setTypeCode(columnType); col.setPrecisionRadix(columnPrecision); col.setScale(columnScale);
col.setName(getValueAsString(columnData, "COLUMN_NAME", availableColumns, "UNKNOWN")); col.setTypeCode(getValueAsInt(columnData, "DATA_TYPE", availableColumns, java.sql.Types.OTHER)); col.setPrecisionRadix(getValueAsInt(columnData, "NUM_PREC_RADIX", availableColumns, 10)); col.setScale(getValueAsInt(columnData, "DECIMAL_DIGITS", availableColumns, 0));
    private List getColumnsForTable(String tableName) throws SQLException {        List columnInfoColumns = new Vector();        DatabaseMetaData dbmd = connection.getMetaData();        List columns = new Vector();        ResultSet columnData = null;        List primaryKeys = getPrimaryKeysForTable(tableName);        try {            columnData = dbmd.getColumns(catalog, schema, tableName, null);            ResultSetMetaData rsmd = columnData.getMetaData();            for (int i = 0; i < rsmd.getColumnCount(); i++) {                columnInfoColumns.add(rsmd.getColumnName(i + 1));            }            while (columnData.next()) {                /* table catalog (may be null) */                String columnTableCatalog =                    columnInfoColumns.contains("TABLE_CAT")                        ? columnData.getString("TABLE_CAT")                        : "";                /* table schema (may be null) */                String columnTableSchema =                    columnInfoColumns.contains("TABLE_SCHEM")                        ? columnData.getString("TABLE_SCHEM")                        : "";                /* column name */                String columnName =                    columnInfoColumns.contains("COLUMN_NAME")                        ? columnData.getString("COLUMN_NAME")                        : "UNKNOWN";                /* SQL type from java.sql.Types */                int columnType =                    columnInfoColumns.contains("DATA_TYPE")                        ? columnData.getInt("DATA_TYPE")                        : java.sql.Types.OTHER;                /*                  * Data source dependent type name, for a UDT the type name is                  * fully qualified                 */                String columnDbName =                    columnInfoColumns.contains("TYPE_NAME")                        ? columnData.getString("TYPE_NAME")                        : null;                /*                  * column size.  For char or date types this is the maximum                  * number of characters, for numeric or decimal types this is                  * precision.                 */                String columnSize =                    columnInfoColumns.contains("COLUMN_SIZE")                        ? columnData.getString("COLUMN_SIZE")                        : (String)defaultSizes.get(new Integer(columnType));                /* the number of fractional digits */                int columnScale =                    columnInfoColumns.contains("DECIMAL_DIGITS")                        ? columnData.getInt("DECIMAL_DIGITS")                        : 0;                /* Radix (typically either 10 or 2) */                int columnPrecision =                    columnInfoColumns.contains("NUM_PREC_RADIX")                        ? columnData.getInt("NUM_PREC_RADIX")                        : 10;                /*                  * is NULL allowed.                 * columnNoNulls - might not allow NULL values                 * columnNullable - definitely allows NULL values                 * columnNullableUnknown - nullability unknown                 */                int columnNullable =                    columnInfoColumns.contains("NULLABLE")                        ? columnData.getInt("NULLABLE")                        : ResultSetMetaData.columnNullableUnknown;                /* comment describing column (may be null) */                String columnRemarks =                    columnInfoColumns.contains("REMARKS")                        ? columnData.getString("REMARKS")                        : "";                /* default value (may be null) */                String columnDefaultValue =                    columnInfoColumns.contains("COLUMN_DEF")                        ? columnData.getString("COLUMN_DEF")                        : null;                /* for char types the maximum number of bytes in the column */                int columnCharOctetLength =                    columnInfoColumns.contains("CHAR_OCTET_LENGTH")                        ? columnData.getInt("CHAR_OCTET_LENGTH")                        : 0;                /* index of column in table (starting at 1) */                int columnOrdinalPosition =                    columnInfoColumns.contains("ORDINAL_POSITION")                        ? columnData.getInt("ORDINAL_POSITION")                        : 0;                /*                 * "NO" means column definitely does not allow NULL values;                 * "YES" means the column might allow NULL values;                 * An empty string means nobody knows.                 * We make the assumption that "NO" means no, anything else means                 * yes.                 * jmm - sometimes (not always) jTDS/mssql would return "NO ", which doesn't match "NO", hence the trim                 */                boolean columnIsNullable = false;                String isNullableValue = columnInfoColumns.contains("IS_NULLABLE") ? columnData.getString("IS_NULLABLE") : null;                if ( isNullableValue != null ) isNullableValue = isNullableValue.trim();                if ( "NO".equalsIgnoreCase(isNullableValue) ) {                    columnIsNullable = false;                }                else {                    columnIsNullable = true;                }                /*                  * catalog of table that is the scope of a reference attribute                  * (null if DATA_TYPE isn't REF)                  */                String columnScopeCatalog =                    columnInfoColumns.contains("SCOPE_CATLOG")                        ? columnData.getString("SCOPE_CATLOG")                        : null;                /*                  * schema of table that is the scope of a reference attribute                  * (null if DATA_TYPE isn't REF)                  */                String columnScopeSchema =                    columnInfoColumns.contains("SCOPE_SCHEMA")                        ? columnData.getString("SCOPE_SCHEMA")                        : null;                /*                  * table name that is the scope of a reference attribute                  * (null if DATA_TYPE isn't REF)                  */                String columnScopeTable =                    columnInfoColumns.contains("SCOPE_TABLE")                        ? columnData.getString("SCOPE_TABLE")                        : null;                /*                  * source type of a distinct type or user-generated Ref type,                 * SQL type from java.sql.Types (null if DATA_TYPE isn't DISTINCT                  * or user-generated REF)                 */                short columnSourceDataType =                    columnInfoColumns.contains("SOURCE_DATA_TYPE")                        ? columnData.getShort("SOURCE_DATA_TYPE")                        : 0;                Column col = new Column();                col.setName(columnName);                col.setTypeCode(columnType);                col.setPrecisionRadix(columnPrecision);                col.setScale(columnScale);                // we're setting the size after the precision and radix in case                // the database prefers to return them in the size value                 col.setSize(columnSize);                col.setRequired(!columnIsNullable);                if (primaryKeys.contains(col.getName())) {                    col.setPrimaryKey(true);                }                else {                    col.setPrimaryKey(false);                }                //sometimes the default comes back with parenthesis around it (jTDS/mssql)                if ( columnDefaultValue != null ) {                    Matcher m = defaultPattern.matcher(columnDefaultValue);                    if ( m.matches() ) {                        columnDefaultValue = m.group(1);                    }                    col.setDefaultValue(columnDefaultValue);                }                columns.add(col);            }            return columns;        }        finally {            if (columnData != null) {                columnData.close();            }        }    }