Example usage for java.sql PreparedStatement setNull

List of usage examples for java.sql PreparedStatement setNull

Introduction

In this page you can find the example usage for java.sql PreparedStatement setNull.

Prototype

void setNull(int parameterIndex, int sqlType) throws SQLException;

Source Link

Document

Sets the designated parameter to SQL NULL.

Usage

From source file:org.efaps.db.wrapper.AbstractSQLInsertUpdate.java

/**
 * Defines a new column <code>_columnName</code> with {@link Long}
 * <code>_value</code> within this SQL insert / update statement.
 *
 * @param _columnName   name of the column
 * @param _value        value of the column
 * @return this SQL statement//from   w w w.j  a  v  a  2 s . co m
 */
@SuppressWarnings("unchecked")
public STMT column(final String _columnName, final Long _value) {
    this.columnWithValues.add(new AbstractSQLInsertUpdate.AbstractColumnWithValue<Long>(_columnName, _value) {
        @Override
        public void set(final int _index, final PreparedStatement _stmt) throws SQLException {
            if (getValue() == null) {
                _stmt.setNull(_index, Types.BIGINT);
            } else {
                _stmt.setLong(_index, getValue());
            }
        }
    });
    return (STMT) this;
}

From source file:nl.strohalm.cyclos.utils.hibernate.TimePeriodType.java

public void nullSafeSet(final PreparedStatement st, final Object value, final int index,
        final SessionImplementor session) throws HibernateException, SQLException {
    final TimePeriod timePeriod = (TimePeriod) value;
    Integer number = null;/*from w  w  w .j  av  a 2 s .  c o m*/
    TimePeriod.Field field = null;
    if (timePeriod == null || timePeriod.getField() == null) {
        st.setNull(index + NUMBER, Types.INTEGER);
        st.setNull(index + FIELD, Types.INTEGER);
    } else {
        number = timePeriod.getNumber();
        field = timePeriod.getField();
        st.setInt(index + NUMBER, number);
        st.setInt(index + FIELD, field.getCalendarValue());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Binding " + number + " to parameter: " + (index + NUMBER));
        LOG.debug("Binding " + field + " to parameter: " + (index + FIELD));
    }
}

From source file:data.DefaultExchanger.java

protected void setNullableLong(PreparedStatement ps, short index, JsonNode node, String column)
        throws SQLException {
    if (node.get(column).isNull()) {
        ps.setNull(index, Types.BIGINT);
    } else {/*from   w  w w  . jav a 2s.c  om*/
        ps.setLong(index, node.get(column).longValue());
    }
}

From source file:nl.strohalm.cyclos.utils.hibernate.AmountType.java

public void nullSafeSet(final PreparedStatement ps, final Object object, final int index)
        throws HibernateException, SQLException {
    final Amount amount = (Amount) object;
    BigDecimal value = null;/*w  w  w .j a v  a2 s  .  c  o  m*/
    Amount.Type type = null;
    if (amount != null) {
        value = amount.getValue();
        type = amount.getType();
    }
    if (value == null) {
        ps.setNull(index, Types.NUMERIC);
    } else {
        ps.setBigDecimal(index, value);
    }
    if (type == null) {
        ps.setNull(index + 1, Types.CHAR);
    } else {
        ps.setString(index + 1, type.getValue());
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Binding " + value + " to parameter: " + (index));
        LOG.debug("Binding " + (type == null ? null : type.getValue()) + " to parameter: " + (index + 1));
    }

}

From source file:org.efaps.db.wrapper.AbstractSQLInsertUpdate.java

/**
 * Defines a new column <code>_columnName</code> with {@link BigDecimal}
 * <code>_value</code> within this SQL insert / update statement.
 *
 * @param _columnName   name of the column
 * @param _value        value of the column
 * @return this SQL statement//from   ww  w.j  a  v  a2s  . c  o m
 */
@SuppressWarnings("unchecked")
public STMT column(final String _columnName, final BigDecimal _value) {
    this.columnWithValues
            .add(new AbstractSQLInsertUpdate.AbstractColumnWithValue<BigDecimal>(_columnName, _value) {
                @Override
                public void set(final int _index, final PreparedStatement _stmt) throws SQLException {
                    if (getValue() == null) {
                        _stmt.setNull(_index, Types.DECIMAL);
                    } else {
                        _stmt.setBigDecimal(_index, getValue());
                    }
                }
            });
    return (STMT) this;
}

From source file:org.efaps.db.wrapper.AbstractSQLInsertUpdate.java

/**
 * Defines a new column <code>_columnName</code> with {@link Timestamp}
 * <code>_value</code> within this SQL insert / update statement.
 *
 * @param _columnName   name of the column
 * @param _value        value of the column
 * @return this SQL statement//from   ww  w  . j a va2s. c  o m
 */
@SuppressWarnings("unchecked")
public STMT column(final String _columnName, final Timestamp _value) {
    this.columnWithValues
            .add(new AbstractSQLInsertUpdate.AbstractColumnWithValue<Timestamp>(_columnName, _value) {
                @Override
                public void set(final int _index, final PreparedStatement _stmt) throws SQLException {
                    if (getValue() == null) {
                        _stmt.setNull(_index, Types.TIMESTAMP);
                    } else {
                        _stmt.setTimestamp(_index, getValue());
                    }
                }
            });
    return (STMT) this;
}

From source file:org.efaps.db.wrapper.AbstractSQLInsertUpdate.java

/**
 * Defines a new column <code>_columnName</code> with {@link Boolean}
 * <code>_value</code> within this SQL insert / update statement.
 *
 * @param _columnName   name of the column
 * @param _value        value of the column
 * @return this SQL statement/* ww w.ja v  a 2 s .  c o  m*/
 */
@SuppressWarnings("unchecked")
public STMT column(final String _columnName, final boolean _value) {
    this.columnWithValues
            .add(new AbstractSQLInsertUpdate.AbstractColumnWithValue<Boolean>(_columnName, _value) {
                @Override
                public void set(final int _index, final PreparedStatement _stmt) throws SQLException {
                    if (getValue() == null) {
                        _stmt.setNull(_index, Types.BOOLEAN);
                    } else {
                        _stmt.setBoolean(_index, getValue());
                    }
                }
            });
    return (STMT) this;
}

From source file:org.efaps.db.wrapper.AbstractSQLInsertUpdate.java

/**
 * Defines a new column <code>_columnName</code> with {@link Long}
 * <code>_value</code> within this SQL insert / update statement.
 *
 * @param _columnName   name of the column
 * @param _value        value of the column
 * @return this SQL statement/*from  ww w  .j  av a 2s.co m*/
 */
@SuppressWarnings("unchecked")
public STMT column(final String _columnName, final Integer _value) {
    this.columnWithValues
            .add(new AbstractSQLInsertUpdate.AbstractColumnWithValue<Integer>(_columnName, _value) {
                @Override
                public void set(final int _index, final PreparedStatement _stmt) throws SQLException {
                    if (getValue() == null) {
                        _stmt.setNull(_index, Types.BIGINT);
                    } else {
                        _stmt.setInt(_index, getValue());
                    }
                }
            });
    return (STMT) this;
}

From source file:net.solarnetwork.node.dao.jdbc.consumption.JdbcConsumptionDatumDao.java

@Override
protected void setStoreStatementValues(ConsumptionDatum datum, PreparedStatement ps) throws SQLException {
    int col = 1;//from w w w .  j  a va 2s . c  o  m
    ps.setTimestamp(col++, new java.sql.Timestamp(
            datum.getCreated() == null ? System.currentTimeMillis() : datum.getCreated().getTime()));
    ps.setString(col++, datum.getSourceId() == null ? "" : datum.getSourceId());
    if (datum.getLocationId() == null) {
        ps.setNull(col++, Types.BIGINT);
    } else {
        ps.setLong(col++, datum.getLocationId());
    }
    if (datum.getWatts() == null) {
        ps.setNull(col++, Types.INTEGER);
    } else {
        ps.setInt(col++, datum.getWatts());
    }
    if (datum.getWattHourReading() == null) {
        ps.setNull(col++, Types.BIGINT);
    } else {
        ps.setLong(col++, datum.getWattHourReading());
    }
}

From source file:org.LexGrid.util.sql.DBUtility.java

/**
 * Sets booleans properly in the LexGrid world.
 * //from   www.  j ava  2 s.  c  om
 * @param statement
 * @param colNumber
 * @param value
 * @param isSqlLite
 *            - using sqlLite tables? Set to true if the answer is yes.
 * @param databaseType
 *            - optional. Set to null or "" if you don't know it.
 * @throws SQLException
 */
public static void setBooleanOnPreparedStatment(PreparedStatement statement, int colNumber, Boolean value,
        boolean isSqlLite, String databaseType) throws SQLException {
    if (databaseType == null || databaseType.length() == 0) {
        databaseType = statement.getConnection().getMetaData().getDatabaseProductName();
    }
    // This has been tested (and works correctly) on mysql (using tinyint
    // with 1 and 0), postgres (using
    // bool)
    // and Access using both a Text of (True or False) and yes/no.
    if (value == null) {
        // mysql lite (on access) uses yesno's for booleans (which don't
        // support null), while regular sql
        // doesn't.
        if (isSqlLite && databaseType.equals("ACCESS")) {
            statement.setBoolean(colNumber, false);
        }
        // the new postgres driver doesn't allow you to use the setString to
        // null trick.
        else if (databaseType.equals("PostgreSQL")) {
            statement.setNull(colNumber, java.sql.Types.BOOLEAN);
        }
        // most other databases let you set null on a string, even if it
        // isn't a string type.
        else {
            statement.setString(colNumber, null);
        }
    } else {
        // sql on format on access, and mysql both use strings instead of
        // booleans (to support null)
        if ((databaseType.equals("ACCESS") && !isSqlLite)) {
            statement.setString(colNumber, value.booleanValue() + "");
        } else if (databaseType.equals("MySQL")) {
            statement.setInt(colNumber, (value.booleanValue() ? 1 : 0));
        } else {
            statement.setBoolean(colNumber, value.booleanValue());
        }
    }
}