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.kawanfw.test.api.client.InsertPreparedStatementTest.java

/**
 * Do a 100 row insert inside a loop/*from   ww  w .j av  a2 s.  c  om*/
 * 
 * @param connection
 *            the AceQL Connection
 * 
 * @param useRawExecute
 *            if true, we will insert using execute()
 * 
 * @throws Exception
 *             it any Exception occurs
 */
public static void insertLoopPrepStatement(Connection connection, int numberToInsert, boolean useRawExecute)
        throws Exception {
    // We can now use our Remote JDBC Connection as a regular Connection!

    if (!useRawExecute) {
        connection.setAutoCommit(false);
    }

    // We will do all our remote insert in a SQL Transaction
    try {
        // 1) First create a Customer
        String sql = "insert into customer values ( ?, ?, ?, ?, ?, ?, ?, ? )";

        MessageDisplayer.display("Inserting " + numberToInsert + " customers...");

        for (int customerId = 1; customerId < numberToInsert + 1; customerId++) {
            PreparedStatement prepStatement = connection.prepareStatement(sql);
            prepStatement.setInt(1, customerId);
            prepStatement.setString(2, "Sir");
            // prepStatement.setString(3, "Jol_" + customerId);
            prepStatement.setNull(3, Types.VARCHAR);
            prepStatement.setString(4, "Smith_" + customerId);
            prepStatement.setString(5, customerId + ", Csar Avenue");
            prepStatement.setString(6, "JavaLand_" + customerId);
            prepStatement.setString(7, customerId + "45");
            prepStatement.setString(8, customerId + "-12345678");

            if (useRawExecute) {
                prepStatement.execute();
            } else {
                prepStatement.executeUpdate();
            }

            prepStatement.close();
        }

        MessageDisplayer.display(new Date() + " Before Commit...");

        // We do either everything in a single transaction or nothing

        if (!useRawExecute) {
            connection.commit(); // Commit is propagated on Server
        }

        MessageDisplayer.display(new Date() + " Remote Commit Done on AceQL Server!");
    } catch (Exception e) {
        e.printStackTrace();
        if (!useRawExecute) {
            connection.rollback();
        }
        throw e;
    } finally {
        connection.setAutoCommit(true);
    }

}

From source file:org.openadaptor.auxil.connector.jdbc.writer.map.AbstractMapWriter.java

/**
 * Setup arguments for a prepared statement.
 * <br>/*from   w  w  w . j  a va2s . c o  m*/
 * Note that it only requires the SQL type as setObject(x,null) cannot be used.
 * Seems silly, since the driver must be able to determine the type itself
 * for the null call.
 * @throws SQLException
 */
protected static void setArguments(PreparedStatement ps, Map map, String[] colNames, int[] sqlTypes)
        throws SQLException {
    for (int i = 1; i <= colNames.length; i++) {
        Object value = map.get(colNames[i - 1]); //Loop isn't zero based
        if (value != null || (sqlTypes == null)) { //Worth a try if sqlTypes ain't available! 
            ps.setObject(i, value); //Don't need type here
        } else {
            ps.setNull(i, sqlTypes[i - 1]);
        }
        if (log.isDebugEnabled()) {
            log.debug("PreparedStatement arg [" + i + "] " + value);
        }
    }
}

From source file:org.castor.jdo.engine.SQLTypeInfos.java

/**
 * Set given value on given PreparedStatement at given index with given SQL type.
 * // w  w w  .j  a v  a2s  .  co m
 * @param stmt The PreparedStatement to set value on.
 * @param index The index of the value in the PreparedStatement.
 * @param value The value to set.
 * @param sqlType The SQL type of the value.
 */
public static void setValue(final PreparedStatement stmt, final int index, final Object value,
        final int sqlType) {
    try {
        if (value == null) {
            stmt.setNull(index, sqlType);
        } else {
            // Special processing for BLOB and CLOB types, because they are mapped
            // by Castor to java.io.InputStream and java.io.Reader, respectively,
            // while JDBC driver expects java.sql.Blob and java.sql.Clob.
            switch (sqlType) {
            case Types.FLOAT:
            case Types.DOUBLE:
                stmt.setDouble(index, ((Double) value).doubleValue());
                break;
            case Types.REAL:
                stmt.setFloat(index, ((Float) value).floatValue());
                break;
            case Types.TIME:
                final Time time = value instanceof java.util.Date ? new Time(((java.util.Date) value).getTime())
                        : null;
                stmt.setTime(index, time != null ? time : (Time) value, getCalendar());
                break;
            case Types.DATE:
                final Date date = value instanceof java.util.Date ? new Date(((java.util.Date) value).getTime())
                        : null;
                stmt.setDate(index, date != null ? date : (Date) value);
                break;
            case Types.TIMESTAMP:
                final Timestamp timestamp = value instanceof java.util.Date
                        ? new Timestamp(((java.util.Date) value).getTime())
                        : null;
                stmt.setTimestamp(index, timestamp != null ? timestamp : (Timestamp) value, getCalendar());
                break;
            case Types.BLOB:
                try {
                    InputStream stream;
                    if (value instanceof byte[]) {
                        stream = new ByteArrayInputStream((byte[]) value);
                    } else {
                        stream = (InputStream) value;
                    }
                    stmt.setBinaryStream(index, stream, stream.available());
                } catch (IOException ex) {
                    throw new SQLException(ex.toString());
                }
                break;
            case Types.CLOB:
                if (value instanceof String) {
                    stmt.setCharacterStream(index, new StringReader((String) value),
                            Math.min(((String) value).length(), Integer.MAX_VALUE));
                } else {
                    stmt.setCharacterStream(index, ((Clob) value).getCharacterStream(),
                            (int) Math.min(((Clob) value).length(), Integer.MAX_VALUE));
                }
                break;
            default:
                stmt.setObject(index, value, sqlType);
                break;
            }
        }
    } catch (SQLException ex) {
        LOG.error("Unexpected SQL exception: ", ex);
    }
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * set a java.lang.Long value in the given preparedStatement object, or set it to null if the
 * given Long is null// ww w  .  j av  a  2s. c  o  m
 *
 * @param stmt
 * @param value
 * @param index
 * @throws SQLException
 */
public static void setLong(PreparedStatement stmt, Long value, int index) throws SQLException {
    if (value == null) {
        stmt.setNull(index, Types.BIGINT);
        return;
    }
    stmt.setLong(index, value);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * set a java.lang.Integer value in the given preparedStatement object, or set it to null if the
 * given Integer is null//w w w.j a v a2 s  .c  o  m
 *
 * @param stmt
 * @param value
 * @param index
 * @throws SQLException
 */
public static void setShort(PreparedStatement stmt, Short value, int index) throws SQLException {
    if (value == null) {
        stmt.setNull(index, Types.SMALLINT);
        return;
    }
    stmt.setShort(index, value);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * set a java.util.Date value in the given PreparedStatement object, or set it to null if the
 * given Date is null// w w  w  . j  a  va2s  .c  o m
 *
 * @param stmt
 * @param value
 * @param index
 * @throws SQLException
 */
public static void setDate(PreparedStatement stmt, Date value, int index) throws SQLException {
    if (value == null) {
        stmt.setNull(index, Types.TIMESTAMP);
        return;
    }
    Timestamp ts = new Timestamp(value.getTime());
    stmt.setTimestamp(index, ts);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

public static void setDouble(PreparedStatement stmt, Double value, int index) throws SQLException {
    if (value == null) {
        stmt.setNull(index, Types.DOUBLE);
        return;/*from w ww .  ja v a  2s.  co  m*/
    }
    stmt.setDouble(index, value);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

public static void setBoolean(PreparedStatement stmt, Boolean value, int index) throws SQLException {
    if (value == null) {
        stmt.setNull(index, Types.BOOLEAN);
        return;// w ww. jav a  2  s . com
    }
    stmt.setBoolean(index, value);
}

From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java

/**
 * set a java.lang.Integer value in the given preparedStatement object, or set it to null if the
 * given Integer is null// w  w  w .  j  av a2  s  . c o  m
 *
 * @param stmt
 * @param value
 * @param index
 * @throws SQLException
 */
public static void setInteger(PreparedStatement stmt, Integer value, int index) throws SQLException {
    if (value == null) {
        stmt.setNull(index, Types.INTEGER);
        return;
    }
    stmt.setInt(index, value);
}

From source file:org.daxplore.producer.daxplorelib.raw.RawMeta.java

protected static void addColumnMeta(PreparedStatement stmt, String column, String longname, String qtext,
        String qtype, String spsstype, String valuelabels, String measure) throws SQLException {

    if (column != null)
        stmt.setString(1, column);/*  www.ja  v a 2  s .c o m*/
    else
        throw new NullPointerException();

    if (longname != null)
        stmt.setString(2, longname);
    else
        stmt.setNull(2, java.sql.Types.VARCHAR);

    if (qtext != null)
        stmt.setString(3, qtext);
    else
        stmt.setNull(3, java.sql.Types.VARCHAR);

    if (qtype != null)
        stmt.setString(4, qtype);
    else
        stmt.setNull(4, java.sql.Types.VARCHAR);

    if (spsstype != null)
        stmt.setString(5, spsstype);
    else
        stmt.setNull(5, java.sql.Types.VARCHAR);

    if (valuelabels != null)
        stmt.setString(6, valuelabels);
    else
        stmt.setNull(6, java.sql.Types.VARCHAR);

    if (measure != null)
        stmt.setString(7, measure);
    else
        stmt.setNull(7, java.sql.Types.VARCHAR);

    stmt.executeUpdate();
}