Example usage for java.sql ResultSetMetaData isNullable

List of usage examples for java.sql ResultSetMetaData isNullable

Introduction

In this page you can find the example usage for java.sql ResultSetMetaData isNullable.

Prototype

int isNullable(int column) throws SQLException;

Source Link

Document

Indicates the nullability of values in the designated column.

Usage

From source file:org.apache.kylin.rest.util.HiveReroute.java

private void extractColumnMetadata(ResultSet resultSet, List<SelectedColumnMeta> columnMetas)
        throws SQLException {
    ResultSetMetaData metaData = null;
    int columnCount = 0;

    metaData = resultSet.getMetaData();/*from  www.  ja  v a 2s .c  o m*/
    columnCount = metaData.getColumnCount();

    // fill in selected column meta
    for (int i = 1; i <= columnCount; ++i) {
        columnMetas.add(new SelectedColumnMeta(metaData.isAutoIncrement(i), metaData.isCaseSensitive(i), false,
                metaData.isCurrency(i), metaData.isNullable(i), false, metaData.getColumnDisplaySize(i),
                metaData.getColumnLabel(i), metaData.getColumnName(i), null, null, null,
                metaData.getPrecision(i), metaData.getScale(i), metaData.getColumnType(i),
                metaData.getColumnTypeName(i), metaData.isReadOnly(i), false, false));
    }
}

From source file:org.apache.kylin.rest.adhoc.AdHocRunnerJdbcImpl.java

@Override
public void executeQuery(String query, List<List<String>> results, List<SelectedColumnMeta> columnMetas)
        throws Exception {
    Statement statement = null;/*  w w  w.j a  va2 s. c  o m*/
    Connection connection = this.getConnection();
    ResultSet resultSet = null;

    try {
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);
        extractResults(resultSet, results);
    } catch (SQLException sqlException) {
        throw sqlException;
    }

    //extract column metadata
    ResultSetMetaData metaData = null;
    int columnCount = 0;
    try {
        metaData = resultSet.getMetaData();
        columnCount = metaData.getColumnCount();

        // fill in selected column meta
        for (int i = 1; i <= columnCount; ++i) {
            columnMetas.add(new SelectedColumnMeta(metaData.isAutoIncrement(i), metaData.isCaseSensitive(i),
                    false, metaData.isCurrency(i), metaData.isNullable(i), false,
                    metaData.getColumnDisplaySize(i), metaData.getColumnLabel(i), metaData.getColumnName(i),
                    null, null, null, metaData.getPrecision(i), metaData.getScale(i), metaData.getColumnType(i),
                    metaData.getColumnTypeName(i), metaData.isReadOnly(i), false, false));
        }

    } catch (SQLException sqlException) {
        throw sqlException;
    }

    closeConnection(connection);
}

From source file:at.ac.univie.isc.asio.engine.sql.WebRowSetWriter.java

private void columnDefinition(final int idx, final ResultSetMetaData context)
        throws XMLStreamException, SQLException {
    // @formatter:off
    xml.writeStartElement(WRS, "column-definition");
    tag("column-index", idx);
    tag("auto-increment", context.isAutoIncrement(idx));
    tag("case-sensitive", context.isCaseSensitive(idx));
    tag("currency", context.isCurrency(idx));
    tag("nullable", context.isNullable(idx));
    tag("signed", context.isSigned(idx));
    tag("searchable", context.isSearchable(idx));
    tag("column-display-size", context.getColumnDisplaySize(idx));
    tag("column-label", context.getColumnLabel(idx));
    tag("column-name", context.getColumnName(idx));
    tag("schema-name", context.getSchemaName(idx));
    tag("column-precision", context.getPrecision(idx));
    tag("column-scale", context.getScale(idx));
    tag("table-name", context.getTableName(idx));
    tag("catalog-name", context.getCatalogName(idx));
    tag("column-type", context.getColumnType(idx));
    tag("column-type-name", context.getColumnTypeName(idx));
    xml.writeEndElement();//from  w ww .  j  ava  2s.c  om
    // @formatter:on
}

From source file:org.acmsl.queryj.customsql.handlers.customsqlvalidation.RetrieveResultPropertiesHandler.java

/**
 * Creates a property from given {@link ResultSetMetaData}.
 * @param metadata the result set metadata.
 * @param index the index.//from  w  w  w .j a  v  a  2s.  c o  m
 * @return the associated {@link Property}.
 * @throws SQLException if the property information is unavailable.
 */
@NotNull
protected Property<String> createPropertyFrom(@NotNull final ResultSetMetaData metadata, final int index)
        throws SQLException {
    @NotNull
    final String t_strColumnName = metadata.getColumnName(index);
    @NotNull
    final String t_strType = metadata.getColumnTypeName(index);
    final boolean t_bNullable = (metadata.isNullable(index) == ResultSetMetaData.columnNullable);

    return new PropertyElement<>(t_strColumnName, t_strColumnName, index, t_strType, t_bNullable);
}

From source file:org.apache.kylin.query.adhoc.PushDownRunnerJdbcImpl.java

@Override
public void executeQuery(String query, List<List<String>> results, List<SelectedColumnMeta> columnMetas)
        throws Exception {
    Statement statement = null;/*from w w  w . j  a  v  a2s .c o  m*/
    Connection connection = this.getConnection();
    ResultSet resultSet = null;

    //extract column metadata
    ResultSetMetaData metaData = null;
    int columnCount = 0;
    try {
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);
        extractResults(resultSet, results);
        metaData = resultSet.getMetaData();
        columnCount = metaData.getColumnCount();

        // fill in selected column meta
        for (int i = 1; i <= columnCount; ++i) {
            columnMetas.add(new SelectedColumnMeta(metaData.isAutoIncrement(i), metaData.isCaseSensitive(i),
                    false, metaData.isCurrency(i), metaData.isNullable(i), false,
                    metaData.getColumnDisplaySize(i), metaData.getColumnLabel(i), metaData.getColumnName(i),
                    null, null, null, metaData.getPrecision(i), metaData.getScale(i), metaData.getColumnType(i),
                    metaData.getColumnTypeName(i), metaData.isReadOnly(i), false, false));
        }

    } catch (SQLException sqlException) {
        throw sqlException;
    } finally {
        DBUtils.closeQuietly(resultSet);
        DBUtils.closeQuietly(statement);
        closeConnection(connection);
    }

}

From source file:org.apache.calcite.test.CalciteAssert.java

private static String typeString(ResultSetMetaData metaData) throws SQLException {
    final List<String> list = new ArrayList<>();
    for (int i = 0; i < metaData.getColumnCount(); i++) {
        list.add(metaData.getColumnName(i + 1) + " " + metaData.getColumnTypeName(i + 1)
                + (metaData.isNullable(i + 1) == ResultSetMetaData.columnNoNulls ? " NOT NULL" : ""));
    }/*  w  w  w.j  a va2  s  . com*/
    return list.toString();
}

From source file:com.kylinolap.rest.service.QueryService.java

/**
 * @param sql// w w  w. j a  v a2 s  .c om
 * @param project
 * @return
 * @throws Exception
 */
private SQLResponse execute(String sql, SQLRequest sqlRequest) throws Exception {
    Connection conn = null;
    Statement stat = null;
    ResultSet resultSet = null;
    List<List<String>> results = new LinkedList<List<String>>();
    List<SelectedColumnMeta> columnMetas = new LinkedList<SelectedColumnMeta>();

    try {
        conn = getOLAPDataSource(sqlRequest.getProject()).getConnection();

        if (sqlRequest instanceof PrepareSqlRequest) {
            PreparedStatement preparedState = conn.prepareStatement(sql);

            for (int i = 0; i < ((PrepareSqlRequest) sqlRequest).getParams().length; i++) {
                setParam(preparedState, i + 1, ((PrepareSqlRequest) sqlRequest).getParams()[i]);
            }

            resultSet = preparedState.executeQuery();
        } else {
            stat = conn.createStatement();
            resultSet = stat.executeQuery(sql);
        }

        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();

        // Fill in selected column meta
        for (int i = 1; i <= columnCount; ++i) {
            columnMetas.add(new SelectedColumnMeta(metaData.isAutoIncrement(i), metaData.isCaseSensitive(i),
                    metaData.isSearchable(i), metaData.isCurrency(i), metaData.isNullable(i),
                    metaData.isSigned(i), metaData.getColumnDisplaySize(i), metaData.getColumnLabel(i),
                    metaData.getColumnName(i), metaData.getSchemaName(i), metaData.getCatalogName(i),
                    metaData.getTableName(i), metaData.getPrecision(i), metaData.getScale(i),
                    metaData.getColumnType(i), metaData.getColumnTypeName(i), metaData.isReadOnly(i),
                    metaData.isWritable(i), metaData.isDefinitelyWritable(i)));
        }

        List<String> oneRow = new LinkedList<String>();

        // fill in results
        while (resultSet.next()) {
            for (int i = 0; i < columnCount; i++) {
                oneRow.add((resultSet.getString(i + 1)));
            }

            results.add(new LinkedList<String>(oneRow));
            oneRow.clear();
        }
    } catch (Exception e) {
        logger.error(e.getLocalizedMessage(), e);
        throw e;
    } finally {
        close(resultSet, stat, conn);
    }

    boolean isPartialResult = false;
    String cube = "";
    long totalScanCount = 0;
    for (OLAPContext ctx : OLAPContext.getThreadLocalContexts()) {
        isPartialResult |= ctx.storageContext.isPartialResultReturned();
        cube = ctx.cubeInstance.getName();
        totalScanCount += ctx.storageContext.getTotalScanCount();
    }

    SQLResponse response = new SQLResponse(columnMetas, results, cube, 0, false, null, isPartialResult);
    response.setTotalScanCount(totalScanCount);

    return response;
}

From source file:org.ensembl.healthcheck.util.DBUtils.java

/**
 * Check if there are any blank entires in a column that is not supposed to
 * be null./*from w  ww. ja v  a 2 s  .  c o m*/
 * 
 * @param con
 *            The database connection to use.
 * @param table
 *            The table to use.
 * @param column
 *            The column to examine.
 * @return An list of the row indices of any blank entries. Will be
 *         zero-length if there are none.
 */
public static List<String> checkBlankNonNull(Connection con, String table, String column) {

    if (con == null) {
        logger.severe("checkBlankNonNull (column): Database connection is null");
        return null;
    }

    List<String> blanks = new ArrayList<String>();
    Statement stmt = null;
    ResultSet rs = null;
    try {
        String sql = "SELECT " + column + " FROM " + table;
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        ResultSetMetaData rsmd = rs.getMetaData();
        while (rs.next()) {
            String columnValue = rs.getString(1);
            // should it be non-null?
            if (rsmd.isNullable(1) == ResultSetMetaData.columnNoNulls) {
                if (StringUtils.isEmpty(columnValue)) {
                    blanks.add(Integer.toString(rs.getRow()));
                }
            }
        }
        rs.close();
        stmt.close();
    } catch (Exception e) {
        throw new SqlUncheckedException("Could not check blanks or nulls", e);
    } finally {
        closeQuietly(rs);
        closeQuietly(stmt);
    }

    return blanks;

}

From source file:org.ensembl.healthcheck.util.DBUtils.java

/**
 * Check all columns of a table for blank entires in columns that are marked
 * as being NOT NULL./*from  ww w . j av  a 2s .  c  o  m*/
 * 
 * @param con
 *            The database connection to use.
 * @param table
 *            The table to use.
 * @return The total number of blank null enums.
 */
public static int checkBlankNonNull(Connection con, String table) {

    if (con == null) {
        logger.severe("checkBlankNonNull (table): Database connection is null");
        return 0;
    }

    int blanks = 0;

    String sql = "SELECT * FROM " + table;
    ResultSet rs = null;
    Statement stmt = null;
    try {
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        ResultSetMetaData rsmd = rs.getMetaData();
        while (rs.next()) {
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                String columnValue = rs.getString(i);
                String columnName = rsmd.getColumnName(i);
                // should it be non-null?
                if (rsmd.isNullable(i) == ResultSetMetaData.columnNoNulls) {
                    if (columnValue == null || columnValue.equals("")) {
                        blanks++;
                        logger.warning("Found blank non-null value in column " + columnName + " in " + table);
                    }
                }
            } // for column
        }
    } catch (Exception e) {
        throw new SqlUncheckedException("Could not check for blank non-nulls", e);
    } finally {
        closeQuietly(rs);
        closeQuietly(stmt);
    }

    return blanks;

}

From source file:org.seasar.dbflute.logic.jdbc.metadata.synonym.DfSynonymExtractorOracle.java

protected List<DfColumnMeta> getSynonymColumns(Connection conn, UnifiedSchema synonymOwner, String synonymName)
        throws SQLException {
    final List<DfColumnMeta> columnList = new ArrayList<DfColumnMeta>();
    Statement st = null;/*from  www . ja v  a  2 s.c o  m*/
    ResultSet rs = null;
    try {
        st = conn.createStatement();
        final String synonymSqlName = synonymOwner.buildSchemaQualifiedName(synonymName);
        final String sql = "select * from " + synonymSqlName + " where 0=1";
        rs = st.executeQuery(sql);
        final ResultSetMetaData metaData = rs.getMetaData();
        int count = metaData.getColumnCount();
        for (int i = 0; i < count; i++) {
            int index = i + 1;
            String columnName = metaData.getColumnName(index);
            int columnType = metaData.getColumnType(index);
            String columnTypeName = metaData.getColumnTypeName(index);
            int precision = metaData.getPrecision(index);
            int scale = metaData.getScale(index);
            int nullableType = metaData.isNullable(index);
            DfColumnMeta column = new DfColumnMeta();
            column.setColumnName(columnName);
            column.setJdbcDefValue(columnType);
            column.setDbTypeName(columnTypeName);
            column.setColumnSize(precision);
            column.setDecimalDigits(scale);
            column.setRequired(nullableType == ResultSetMetaData.columnNoNulls);
            columnList.add(column);
        }
        return columnList;
    } finally {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException ignored) {
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ignored) {
            }
        }
    }
}