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.ambraproject.hibernate.OTMBlobType.java

public void nullSafeSet(PreparedStatement statement, java.lang.Object value, int index)
        throws HibernateException, SQLException {
    if (value == null) {
        statement.setNull(index, Types.BLOB);
    } else {/*from w ww .ja va  2  s.c  om*/
        if (value instanceof org.topazproject.otm.Blob) {
            //TODO: I don't think the following line works
            statement.setBlob(index, ((org.topazproject.otm.Blob) value).getInputStream());
        } else {
            throw new HibernateException("Object not of correct type: " + value.getClass().getName());
        }
    }

}

From source file:org.intelligentsia.utility.jpa.usertype.JSONUserType.java

public void nullSafeSet(final PreparedStatement st, final Object value, final int index)
        throws HibernateException, SQLException {
    if (value == null) {
        st.setNull(index, SQL_TYPES[0]);
    } else {/*from  w  w w  . ja va 2 s  . c  o  m*/
        try {
            st.setString(index, mapper.writeValueAsString(value));
        } catch (final JsonGenerationException e) {
            throw new RuntimeException(e);
        } catch (final JsonMappingException e) {
            throw new RuntimeException(e);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.novartis.opensource.yada.adaptor.OracleAdaptor.java

/**
 * Enables checking for {@link JDBCAdaptor#ORACLE_DATE_FMT} if {@code val} does not conform to {@link JDBCAdaptor#STANDARD_DATE_FMT}
 * @since 5.1.1/*from  ww w . j av a  2  s.  c o  m*/
 */
@Override
protected void setDateParameter(PreparedStatement pstmt, int index, char type, String val) throws SQLException {
    if (EMPTY.equals(val) || val == null) {
        pstmt.setNull(index, java.sql.Types.DATE);
    } else {
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DATE_FMT);
        ParsePosition pp = new ParsePosition(0);
        Date dateVal = sdf.parse(val, pp);
        if (dateVal == null) {
            sdf = new SimpleDateFormat(ORACLE_DATE_FMT);
            pp = new ParsePosition(0);
            dateVal = sdf.parse(val, pp);
        }
        if (dateVal != null) {
            long t = dateVal.getTime();
            java.sql.Date sqlDateVal = new java.sql.Date(t);
            pstmt.setDate(index, sqlDateVal);
        }
    }
}

From source file:com.yahoo.elide.datastores.hibernate5.usertypes.JsonType.java

/**
 * {@inheritDoc}/*from  w w  w  .  ja v a2 s.  c o  m*/
 */
@Override
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index,
        SessionImplementor session) throws HibernateException, SQLException {

    if (value == null) {
        preparedStatement.setNull(index, Types.NULL);
    } else {
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(value);
            preparedStatement.setString(index, json);
        } catch (JsonProcessingException e) {
            throw new HibernateException(
                    "Could not write an instance of the mapped class to a prepared statement.");
        }
    }
}

From source file:com.mtgi.analytics.JdbcBehaviorEventPersisterImpl.java

protected void nullSafeSet(PreparedStatement stmt, int index, Object value, int sqlType) throws SQLException {
    if (value == null)
        stmt.setNull(index, sqlType);
    else//from  ww w  . j av a  2s  .  c  om
        stmt.setObject(index, value, sqlType);
}

From source file:com.novartis.opensource.yada.adaptor.OracleAdaptor.java

/**
* Enables checking for {@link JDBCAdaptor#ORACLE_TIMESTAMP_FMT} if {@code val} does not conform to {@link JDBCAdaptor#STANDARD_TIMESTAMP_FMT}
* @since 5.1.1/*  w  ww . java2  s  .  co  m*/
*/
@Override
protected void setTimestampParameter(PreparedStatement pstmt, int index, char type, String val)
        throws SQLException {
    if (EMPTY.equals(val) || val == null) {
        pstmt.setNull(index, java.sql.Types.DATE);
    } else {
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_TIMESTAMP_FMT);
        ParsePosition pp = new ParsePosition(0);
        Date dateVal = sdf.parse(val, pp);
        if (dateVal == null) {
            sdf = new SimpleDateFormat(ORACLE_TIMESTAMP_FMT);
            pp = new ParsePosition(0);
            dateVal = sdf.parse(val, pp);
        }
        if (dateVal != null) {
            long t = dateVal.getTime();
            java.sql.Timestamp sqlDateVal = new java.sql.Timestamp(t);
            pstmt.setTimestamp(index, sqlDateVal);
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType.java

public void nullSafeSet(PreparedStatement pstmt, Object value, int idx, SessionImplementor session)
        throws SQLException {
    if (value == null) {
        pstmt.setNull(idx, sqlTypes[0]);
    } else {//from w w w. j  a v  a  2s.  c om
        type.nullSafeSet(pstmt, bidiMap.getKey(value), idx, session);
    }
}

From source file:org.jbpm.bpel.persistence.db.type.ElementType.java

public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
    // easy way out: null value
    if (value == null) {
        st.setNull(index, SQL_TYPES[0]);
        if (log.isTraceEnabled())
            log.trace("binding null to parameter: " + index);
    } else {/* w  w  w .j a va  2  s  .co  m*/
        Element element = (Element) value;
        try {
            // create identity transformer
            Transformer idTransformer = XmlUtil.getTransformerFactory().newTransformer();

            // allocate memory result stream
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

            // deflate if requested
            Integer deflateLevel = getXmlDeflateLevel();
            if (deflateLevel != null) {
                // introduce deflater stream
                Deflater deflater = new Deflater(deflateLevel.intValue());
                OutputStream deflaterStream = new DeflaterOutputStream(byteStream, deflater);
                // write element to stream
                idTransformer.transform(new DOMSource(element), new StreamResult(deflaterStream));
                // release resources
                try {
                    deflaterStream.close();
                } catch (IOException e) {
                    // should not happen
                    throw new AssertionError(e);
                }
                deflater.end();
            } else {
                // write element to stream
                idTransformer.transform(new DOMSource(element), new StreamResult(byteStream));
                // noop
                // byteStream.close();
            }

            // extract contents of result stream
            st.setBytes(index, byteStream.toByteArray());
            if (log.isTraceEnabled())
                log.trace("binding '" + byteStream + "' to parameter: " + index);
        } catch (TransformerException e) {
            throw new HibernateException("could not transform to xml stream: " + element, e);
        }
    }
}

From source file:cn.guoyukun.spring.jpa.repository.hibernate.type.ObjectSerializeUserType.java

/**
 * Hibernate??/*  w ww.j  a v  a 2 s . c o m*/
 * ?PreparedStateme??
 */
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    ObjectOutputStream oos = null;
    if (value == null) {
        st.setNull(index, Types.VARCHAR);
    } else {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(value);
            oos.close();

            byte[] objectBytes = bos.toByteArray();
            String hexStr = Hex.encodeHexString(objectBytes);

            st.setString(index, hexStr);
        } catch (Exception e) {
            throw new HibernateException(e);
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:net.ymate.platform.persistence.jdbc.base.AbstractOperator.java

protected void __doSetParameters(PreparedStatement statement) throws SQLException {
    for (int i = 0; i < this.getParameters().size(); i++) {
        SQLParameter _param = this.getParameters().get(i);
        if (_param.getValue() == null) {
            statement.setNull(i + 1, 0);
        } else {/*from  w w w.ja  va  2  s .c  o m*/
            statement.setObject(i + 1, _param.getValue());
        }
    }
}