Example usage for java.sql PreparedStatement setBoolean

List of usage examples for java.sql PreparedStatement setBoolean

Introduction

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

Prototype

void setBoolean(int parameterIndex, boolean x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java boolean value.

Usage

From source file:org.apache.sqoop.common.test.db.DatabaseProvider.java

/**
 * Execute given insert query in a new statement object and return
 * generated IDs.//from  w w  w.  ja  v a  2s.  com
 *
 * @param query Query to execute
 * @return Generated ID.
 */
public Long executeInsertQuery(String query, Object... args) {
    LOG.info("Executing query: " + query);
    ResultSet rs = null;

    try {
        PreparedStatement stmt = databaseConnection.prepareStatement(query,
                PreparedStatement.RETURN_GENERATED_KEYS);
        for (int i = 0; i < args.length; ++i) {
            if (args[i] instanceof String) {
                stmt.setString(i + 1, (String) args[i]);
            } else if (args[i] instanceof Long) {
                stmt.setLong(i + 1, (Long) args[i]);
            } else if (args[i] instanceof Boolean) {
                stmt.setBoolean(i + 1, (Boolean) args[i]);
            } else {
                stmt.setObject(i + 1, args[i]);
            }
        }

        stmt.execute();
        rs = stmt.getGeneratedKeys();
        if (rs.next()) {
            return rs.getLong(1);
        }
    } catch (SQLException e) {
        LOG.error("Error in executing query", e);
        throw new RuntimeException("Error in executing query", e);
    } finally {
        closeResultSetWithStatement(rs);
    }

    return -1L;
}

From source file:org.eclipse.ecr.core.storage.sql.jdbc.dialect.DialectPostgreSQL.java

@Override
public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column)
        throws SQLException {
    switch (column.getJdbcType()) {
    case Types.VARCHAR:
    case Types.CLOB:
        setToPreparedStatementString(ps, index, value, column);
        return;//from   www . j  a v a 2 s . c  o  m
    case Types.BIT:
        ps.setBoolean(index, ((Boolean) value).booleanValue());
        return;
    case Types.SMALLINT:
        ps.setInt(index, ((Long) value).intValue());
        return;
    case Types.INTEGER:
    case Types.BIGINT:
        ps.setLong(index, ((Long) value).longValue());
        return;
    case Types.DOUBLE:
        ps.setDouble(index, ((Double) value).doubleValue());
        return;
    case Types.TIMESTAMP:
        setToPreparedStatementTimestamp(ps, index, value, column);
        return;
    case Types.ARRAY:
        Array array = createArrayOf(Types.VARCHAR, (Object[]) value, ps.getConnection());
        ps.setArray(index, array);
        return;
    case Types.OTHER:
        if (column.getType() == ColumnType.FTSTORED) {
            ps.setString(index, (String) value);
            return;
        }
        throw new SQLException("Unhandled type: " + column.getType());
    default:
        throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
    }
}

From source file:cz.incad.vdkcommon.solr.Indexer.java

public void indexAllOffers() throws Exception {
    Connection conn = DbUtils.getConnection();
    String sql = "SELECT offer,datum, ZaznamOffer.zaznamoffer_id, ZaznamOffer.offer, "
            + "ZaznamOffer.uniqueCode, ZaznamOffer.zaznam, ZaznamOffer.exemplar, "
            + "ZaznamOffer.fields, ZaznamOffer.knihovna, ZaznamOffer.pr_knihovna " + "FROM zaznamOffer "
            + "JOIN offer ON offer.offer_id=zaznamOffer.offer where offer.closed=?";
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.setBoolean(1, true);
    try (ResultSet rs = ps.executeQuery()) {
        while (rs.next()) {
            if (jobData.isInterrupted()) {
                LOGGER.log(Level.INFO, "INDEXER INTERRUPTED");
                break;
            }//w w  w .j av  a 2s.  co  m
            server.add(offerDoc(rs.getInt("offer"), rs.getDate("datum"), rs.getString("uniquecode"),
                    rs.getInt("zaznamoffer_id"), rs.getString("zaznam"), rs.getString("knihovna"),
                    rs.getString("pr_knihovna"), rs.getString("exemplar"), rs.getString("fields")));
            offerIndexed++;
            if (offerIndexed % 100 == 0) {
                server.commit();
                writeStatus();
            }

        }
    }
    server.commit();
}

From source file:com.npstrandberg.simplemq.MessageQueueImp.java

private List<Message> receiveInternal(int limit, boolean delete) {
    if (limit < 1)
        limit = 1;/*from w  w w .j a v  a2s .com*/

    List<Message> messages = new ArrayList<Message>(limit);

    try {

        // 'ORDER BY time' depends on that the host computer times is always right.
        // 'ORDER BY id' what happens with the 'id' when we hit Long.MAX_VALUE?
        PreparedStatement ps = conn.prepareStatement(
                "SELECT LIMIT 0 " + limit + " id, object, body FROM message WHERE read=false ORDER BY id");

        // The lock is making sure, that a SELECT and DELETE/UPDATE is only
        // done by one thread at a time.
        lock.lock();

        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            long id = rs.getLong(1);
            InputStream is = rs.getBinaryStream(2);
            String body = rs.getString(3);

            if (delete) {
                PreparedStatement updateInventory = conn.prepareStatement("DELETE FROM message WHERE id=?");
                updateInventory.setLong(1, id);
                updateInventory.executeUpdate();
            } else {
                PreparedStatement updateInventory = conn
                        .prepareStatement("UPDATE message SET read=? WHERE id=?");
                updateInventory.setBoolean(1, true);
                updateInventory.setLong(2, id);
                updateInventory.executeUpdate();
            }

            MessageWrapper mw = new MessageWrapper();
            mw.id = id;
            mw.body = body;
            if (is != null)
                mw.object = Utils.deserialize(is);

            messages.add(mw);
        }

        ps.close();
    } catch (SQLException e) {
        logger.error(e);
    } finally {
        lock.unlock();
    }

    return messages;
}

From source file:org.forumj.dbextreme.db.dao.FJThreadDao.java

public void update(IFJThread thread, Connection conn) throws IOException, SQLException {
    String updateThreadQuery = getUpdateThreadQuery();
    PreparedStatement st = null;
    try {//w  w w  .j a va 2 s. c o  m
        st = conn.prepareStatement(updateThreadQuery);
        st.setString(1, thread.getHead());
        st.setTimestamp(2, new java.sql.Timestamp(thread.getLastPostTime().getTime()));
        st.setLong(3, thread.getLastPostAuthId());
        st.setString(4, thread.getLastPostNick());
        st.setLong(5, thread.getLastPostId());
        st.setInt(6, thread.getDock().getCode());
        st.setLong(7, thread.getFolderId());
        st.setInt(8, thread.getPcount());
        st.setBoolean(9, thread.isClosed());
        st.setLong(10, thread.getId());
        st.executeUpdate();
    } finally {
        readFinally(null, st);
    }
}

From source file:cz.incad.vdkcommon.solr.Indexer.java

public void indexDocOffers(String uniqueCode)
        throws NamingException, SQLException, IOException, SolrServerException, Exception {
    Connection conn = DbUtils.getConnection();
    try {//from  w  ww  . ja  va  2s  . co m
        String sql = "SELECT offer,datum, ZaznamOffer.zaznamoffer_id, ZaznamOffer.offer, "
                + "ZaznamOffer.uniqueCode, ZaznamOffer.zaznam, ZaznamOffer.exemplar, "
                + "ZaznamOffer.fields, ZaznamOffer.knihovna, ZaznamOffer.pr_knihovna, ZaznamOffer.pr_timestamp "
                + "FROM ZaznamOffer "
                + "JOIN offer ON offer.offer_id=ZaznamOffer.offer where offer.closed=? and ZaznamOffer.uniquecode=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setBoolean(1, true);
        ps.setString(2, uniqueCode);
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                if (jobData.isInterrupted()) {
                    LOGGER.log(Level.INFO, "INDEXER INTERRUPTED");
                    break;
                }
                server.add(offerDoc(rs.getInt("offer"), rs.getDate("datum"), rs.getString("uniquecode"),
                        rs.getInt("zaznamoffer_id"), rs.getString("zaznam"), rs.getString("knihovna"),
                        rs.getString("pr_knihovna"), rs.getString("exemplar"), rs.getString("fields")));

                offerIndexed++;

            }
        }
        server.commit();
    } finally {
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
    }
}

From source file:org.cloudfoundry.identity.uaa.scim.jdbc.JdbcScimUserProvisioning.java

@Override
public ScimUser update(final String id, final ScimUser user) throws InvalidScimResourceException {
    validate(user);//from   w w w  . j  a va  2  s .co m
    logger.debug("Updating user " + user.getUserName());
    final String origin = hasText(user.getOrigin()) ? user.getOrigin() : OriginKeys.UAA;
    final String zoneId = IdentityZoneHolder.get().getId();
    int updated = jdbcTemplate.update(UPDATE_USER_SQL, new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            int pos = 1;
            Timestamp t = new Timestamp(new Date().getTime());
            ps.setInt(pos++, user.getVersion() + 1);
            ps.setTimestamp(pos++, t);
            ps.setString(pos++, user.getUserName());
            ps.setString(pos++, user.getPrimaryEmail());
            ps.setString(pos++, user.getName().getGivenName());
            ps.setString(pos++, user.getName().getFamilyName());
            ps.setBoolean(pos++, user.isActive());
            ps.setString(pos++, extractPhoneNumber(user));
            ps.setBoolean(pos++, user.isVerified());
            ps.setString(pos++, origin);
            ps.setString(pos++, hasText(user.getExternalId()) ? user.getExternalId() : null);
            ps.setString(pos++, user.getSalt());
            ps.setString(pos++, id);
            ps.setInt(pos++, user.getVersion());
            ps.setString(pos++, zoneId);
        }
    });
    ScimUser result = retrieve(id);
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d", id,
                        result.getVersion(), user.getVersion()));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    return result;
}

From source file:cz.incad.vdkcommon.solr.Indexer.java

private String getDocOffers(String uniqueCode)
        throws NamingException, SQLException, IOException, SolrServerException, Exception {
    Connection conn = DbUtils.getConnection();
    try {/*from ww  w.ja  v  a2 s .c  o  m*/
        String sql = "SELECT offer,datum, ZaznamOffer.zaznamoffer_id, ZaznamOffer.offer, "
                + "ZaznamOffer.uniqueCode, ZaznamOffer.zaznam, ZaznamOffer.exemplar, "
                + "ZaznamOffer.fields, ZaznamOffer.knihovna, ZaznamOffer.pr_knihovna, ZaznamOffer.pr_timestamp "
                + "FROM ZaznamOffer "
                + "JOIN offer ON offer.offer_id=ZaznamOffer.offer where offer.closed=? and ZaznamOffer.uniquecode=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setBoolean(1, true);
        ps.setString(2, uniqueCode);
        StringBuilder sb = new StringBuilder();
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                if (jobData.isInterrupted()) {
                    LOGGER.log(Level.INFO, "INDEXER INTERRUPTED");
                    break;
                }
                sb.append(offerXml(rs.getInt("offer"), rs.getDate("datum"), rs.getString("uniquecode"),
                        rs.getInt("zaznamoffer_id"), rs.getString("zaznam"), rs.getString("knihovna"),
                        rs.getString("pr_knihovna"), rs.getString("exemplar"), rs.getString("fields")));

                offerIndexed++;

            }
        }
        return sb.toString();
    } finally {
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
    }
}

From source file:org.atricore.idbus.idojos.dbsessionstore.DbSessionStore.java

protected void update(Connection conn, BaseSession session) throws SQLException {
    final PreparedStatement ps = conn.prepareStatement(_updateDml);
    ps.setString(1, session.getUsername());
    ps.setLong(2, session.getCreationTime());
    ps.setLong(3, session.getLastAccessTime());
    ps.setInt(4, (int) session.getAccessCount());
    ps.setInt(5, session.getMaxInactiveInterval());
    ps.setBoolean(6, session.isValid());
    ps.setString(7, session.getId());/*from  w  w  w  .j  av a  2 s. c o m*/
    ps.execute();

    if (__log.isDebugEnabled())
        __log.debug("Creation, LastAccess: " + session.getCreationTime() + ", " + session.getCreationTime());

    if (__log.isDebugEnabled())
        __log.debug("Session updated: " + session.getId());
}

From source file:org.nuxeo.ecm.core.storage.sql.jdbc.dialect.DialectSQLServer.java

@Override
public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column)
        throws SQLException {
    switch (column.getJdbcType()) {
    case Types.VARCHAR:
    case Types.CLOB:
        setToPreparedStatementString(ps, index, value, column);
        return;//from  ww w .  j  a v a  2s.co m
    case Types.BIT:
        ps.setBoolean(index, ((Boolean) value).booleanValue());
        return;
    case Types.TINYINT:
    case Types.INTEGER:
    case Types.BIGINT:
        ps.setLong(index, ((Number) value).longValue());
        return;
    case Types.DOUBLE:
        ps.setDouble(index, ((Double) value).doubleValue());
        return;
    case Types.TIMESTAMP:
        setToPreparedStatementTimestamp(ps, index, value, column);
        return;
    default:
        throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
    }
}