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:com.amazonbird.announce.ProductMgrImpl.java

private void setProductActiveInactive(long id, boolean active) {
    Connection connection = null;
    PreparedStatement ps = null;

    try {/*from  w  ww  . ja  va  2  s  .c o m*/
        connection = dbMgr.getConnection();
        ps = connection.prepareStatement(SET_PRODUCT_ACTIVE);
        ps.setBoolean(1, active);
        ps.setLong(2, id);
        ps.executeUpdate();
        logger.debug(DBConstants.QUERY_EXECUTION_SUCC + ps.toString());
    } catch (SQLException ex) {
        logger.error(DBConstants.QUERY_EXECUTION_FAIL + ps.toString(), ex);
    } finally {
        dbMgr.closeResources(connection, ps, null);
    }
}

From source file:com.l2jfree.gameserver.model.entity.Couple.java

public Couple(L2Player player1, L2Player player2) {
    int _tempPlayer1Id = player1.getObjectId();
    int _tempPlayer2Id = player2.getObjectId();

    _player1Id = _tempPlayer1Id;/* ww  w .j  a  v  a 2s  .co  m*/
    _player2Id = _tempPlayer2Id;

    _affiancedDate = System.currentTimeMillis();
    _weddingDate = System.currentTimeMillis();

    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;
        _id = IdFactory.getInstance().getNextId();
        statement = con.prepareStatement(
                "INSERT INTO couples (id, player1Id, player2Id, maried, affiancedDate, weddingDate) VALUES (?, ?, ?, ?, ?, ?)");
        statement.setInt(1, _id);
        statement.setInt(2, _player1Id);
        statement.setInt(3, _player2Id);
        statement.setBoolean(4, false);
        statement.setLong(5, _affiancedDate);
        statement.setLong(6, _weddingDate);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:net.sf.l2j.gameserver.model.entity.Couple.java

public Couple(L2PcInstance player1, L2PcInstance player2) {
    int _tempPlayer1Id = player1.getObjectId();
    int _tempPlayer2Id = player2.getObjectId();
    _player1Id = _tempPlayer1Id;/*from www . ja  va2  s  .  c  o m*/
    _player2Id = _tempPlayer2Id;
    _affiancedDate = Calendar.getInstance();
    _affiancedDate.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
    _weddingDate = Calendar.getInstance();
    _weddingDate.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
    java.sql.Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement;
        _Id = IdFactory.getInstance().getNextId();
        statement = con.prepareStatement(
                "INSERT INTO couples (id, player1Id, player2Id, maried, affiancedDate, weddingDate) VALUES (?, ?, ?, ?, ?, ?)");
        statement.setInt(1, _Id);
        statement.setInt(2, _player1Id);
        statement.setInt(3, _player2Id);
        statement.setBoolean(4, false);
        statement.setLong(5, _affiancedDate.getTimeInMillis());
        statement.setLong(6, _weddingDate.getTimeInMillis());
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error("", e);
    } finally {
        try {
            con.close();
        } catch (Exception e) {
        }
    }
}

From source file:fr.gael.dhus.database.liquibase.CopyProductImages.java

@Override
public void execute(Database database) throws CustomChangeException {
    PreparedStatement products = null;
    ResultSet products_res = null;
    JdbcConnection db_connection = (JdbcConnection) database.getConnection();
    try {// ww w .  ja v a  2s.  c om
        products = db_connection.prepareStatement("SELECT ID,QUICKLOOK,THUMBNAIL FROM PRODUCTS");
        products_res = products.executeQuery();
        while (products_res.next()) {
            PreparedStatement copy_blob_stmt = null;
            ResultSet generated_key_res = null;
            try {
                Blob ql = (Blob) products_res.getObject("QUICKLOOK");
                Blob th = (Blob) products_res.getObject("THUMBNAIL");
                Long pid = products_res.getLong("ID");

                // No images: add false flags
                if ((ql == null) && (th == null)) {
                    PreparedStatement product_flags_stmt = null;
                    // Add related flags
                    try {
                        product_flags_stmt = db_connection.prepareStatement(
                                "UPDATE PRODUCTS SET THUMBNAIL_FLAG=?,QUICKLOOK_FLAG=? " + "WHERE ID=?");
                        product_flags_stmt.setBoolean(1, false);
                        product_flags_stmt.setBoolean(2, false);
                        product_flags_stmt.setLong(3, pid);
                        product_flags_stmt.execute();
                    } finally {
                        if (product_flags_stmt != null)
                            try {
                                product_flags_stmt.close();
                            } catch (Exception e) {
                                logger.warn("Cannot close Statement !");
                            }
                    }
                    continue;
                }

                copy_blob_stmt = db_connection.prepareStatement(
                        "INSERT INTO PRODUCT_IMAGES (QUICKLOOK,THUMBNAIL) " + "VALUES (?,?)",
                        Statement.RETURN_GENERATED_KEYS);

                copy_blob_stmt.setBlob(1, ql);
                copy_blob_stmt.setBlob(2, th);
                copy_blob_stmt.execute();

                generated_key_res = copy_blob_stmt.getGeneratedKeys();
                if (generated_key_res.next()) {
                    PreparedStatement set_product_image_id_stmt = null;
                    Long iid = generated_key_res.getLong(1);

                    // Add ProductImages "IMAGES" entry in product
                    try {
                        set_product_image_id_stmt = db_connection
                                .prepareStatement("UPDATE PRODUCTS SET IMAGES_ID=?, THUMBNAIL_FLAG=?, "
                                        + "QUICKLOOK_FLAG=?  WHERE ID=?");
                        set_product_image_id_stmt.setLong(1, iid);
                        set_product_image_id_stmt.setBoolean(2, th != null);
                        set_product_image_id_stmt.setBoolean(3, ql != null);
                        set_product_image_id_stmt.setLong(4, pid);
                        set_product_image_id_stmt.execute();
                    } finally {
                        if (set_product_image_id_stmt != null)
                            try {
                                set_product_image_id_stmt.close();
                            } catch (Exception e) {
                                logger.warn("Cannot close Statement !");
                            }
                    }
                } else {
                    logger.error("Cannot retrieve Image primary key for " + "product ID #"
                            + products_res.getLong("ID"));
                }
            } finally {
                if (generated_key_res != null)
                    try {
                        generated_key_res.close();
                    } catch (Exception e) {
                        logger.warn("Cannot close ResultSet !");
                    }
                if (copy_blob_stmt != null)
                    try {
                        copy_blob_stmt.close();
                    } catch (Exception e) {
                        logger.warn("Cannot close Statement !");
                    }
            }
        }
    } catch (Exception e) {
        throw new CustomChangeException("Cannot move Blobs from product", e);
    } finally {
        if (products_res != null) {
            try {
                products_res.close();
            } catch (Exception e) {
                logger.warn("Cannot close ResultSet !");
            }
        }
        if (products != null) {
            try {
                products.close();
            } catch (Exception e) {
                logger.warn("Cannot close Statement !");
            }
        }
        //if (db_connection!=null) try { db_connection.close (); }
        // catch (Exception e) {}
    }
}

From source file:org.cloudfoundry.identity.uaa.provider.saml.idp.JdbcSamlServiceProviderProvisioning.java

@Override
public SamlServiceProvider update(final SamlServiceProvider serviceProvider) {
    validate(serviceProvider);//w ww  . ja  v a2  s.  c  om
    final String zoneId = IdentityZoneHolder.get().getId();
    jdbcTemplate.update(UPDATE_SERVICE_PROVIDER_SQL, new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            int pos = 1;
            ps.setInt(pos++, serviceProvider.getVersion() + 1);
            ps.setTimestamp(pos++, new Timestamp(new Date().getTime()));
            ps.setString(pos++, serviceProvider.getName());
            ps.setString(pos++, JsonUtils.writeValueAsString(serviceProvider.getConfig()));
            ps.setBoolean(pos++, serviceProvider.isActive());
            ps.setString(pos++, serviceProvider.getId().trim());
            ps.setString(pos++, zoneId);
        }
    });
    return retrieve(serviceProvider.getId());
}

From source file:net.freechoice.model.orm.Map_Profile.java

@Override
public PreparedStatementCreator createInsert(final FC_Profile prof) {

    return new PreparedStatementCreator() {
        @Override//from w  ww . j  a  v  a  2s . co m
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement ps = con.prepareStatement("insert into FC_Profile(" + " id_user_, "
                    + " site_personal," + " name_first, name_last," + " contact_public, "
                    + " gender, date_birth)" + " values( " + prof.id_user_ + ", ?, ?, ?, ?, ?, ?)", RET_ID);
            ps.setString(1, prof.site_personal);
            ps.setString(2, prof.name_first);
            ps.setString(3, prof.name_last);
            ps.setString(4, prof.contact_public);
            ps.setBoolean(5, prof.gender);
            ps.setDate(6, prof.date_birth);
            return ps;
        }
    };
}

From source file:org.schedoscope.metascope.tasks.repository.mysql.impl.TableEntityMySQLRepository.java

public void insertOrUpdatePartial(Connection connection, TableEntity tableEntity) {
    String insertTableSql = "insert into table_entity (table_fqdn, table_name, database_name, url_path_prefix, external_table, "
            + "table_description, storage_format, materialize_once, transformation_type, status) "
            + "values (?, ?, ?, ?, ?, ?, ?, ?, ?) "
            + "on duplicate key update table_fqdn=values(table_fqdn), table_name=values(table_name), database_name=values(database_name), "
            + "url_path_prefix=values(url_path_prefix),external_table=values(external_table), table_description=values(table_description), "
            + "storage_format=values(storage_format), materialize_once=values(materialize_once), transformation_type=values(transformation_type), "
            + "status=values(status)";
    PreparedStatement stmt = null;
    try {//from   www.jav a2 s . c  o m
        stmt = connection.prepareStatement(insertTableSql);
        stmt.setString(1, tableEntity.getFqdn());
        stmt.setString(2, tableEntity.getTableName());
        stmt.setString(3, tableEntity.getDatabaseName());
        stmt.setString(4, tableEntity.getUrlPathPrefix());
        stmt.setBoolean(5, tableEntity.isExternalTable());
        stmt.setString(6, tableEntity.getTableDescription());
        stmt.setString(7, tableEntity.getStorageFormat());
        stmt.setBoolean(8, tableEntity.isMaterializeOnce());
        stmt.setString(9, tableEntity.getTransformationType());
        stmt.setString(10, tableEntity.getStatus());
        stmt.execute();
    } catch (SQLException e) {
        LOG.error("Could not save table", e);
    } finally {
        DbUtils.closeQuietly(stmt);
    }
}

From source file:org.waarp.common.database.data.AbstractDbData.java

/**
 * Set Value into PreparedStatement//from   w  w  w  .j av a2 s  . co  m
 * 
 * @param ps
 * @param value
 * @param rank
 *            >= 1
 * @throws WaarpDatabaseSqlException
 */
static public void setTrueValue(PreparedStatement ps, DbValue value, int rank)
        throws WaarpDatabaseSqlException {
    try {
        switch (value.type) {
        case Types.VARCHAR:
            if (value.value == null) {
                ps.setNull(rank, Types.VARCHAR);
                break;
            }
            ps.setString(rank, (String) value.value);
            break;
        case Types.LONGVARCHAR:
            if (value.value == null) {
                ps.setNull(rank, Types.LONGVARCHAR);
                break;
            }
            ps.setString(rank, (String) value.value);
            break;
        case Types.BIT:
            if (value.value == null) {
                ps.setNull(rank, Types.BIT);
                break;
            }
            ps.setBoolean(rank, (Boolean) value.value);
            break;
        case Types.TINYINT:
            if (value.value == null) {
                ps.setNull(rank, Types.TINYINT);
                break;
            }
            ps.setByte(rank, (Byte) value.value);
            break;
        case Types.SMALLINT:
            if (value.value == null) {
                ps.setNull(rank, Types.SMALLINT);
                break;
            }
            ps.setShort(rank, (Short) value.value);
            break;
        case Types.INTEGER:
            if (value.value == null) {
                ps.setNull(rank, Types.INTEGER);
                break;
            }
            ps.setInt(rank, (Integer) value.value);
            break;
        case Types.BIGINT:
            if (value.value == null) {
                ps.setNull(rank, Types.BIGINT);
                break;
            }
            ps.setLong(rank, (Long) value.value);
            break;
        case Types.REAL:
            if (value.value == null) {
                ps.setNull(rank, Types.REAL);
                break;
            }
            ps.setFloat(rank, (Float) value.value);
            break;
        case Types.DOUBLE:
            if (value.value == null) {
                ps.setNull(rank, Types.DOUBLE);
                break;
            }
            ps.setDouble(rank, (Double) value.value);
            break;
        case Types.VARBINARY:
            if (value.value == null) {
                ps.setNull(rank, Types.VARBINARY);
                break;
            }
            ps.setBytes(rank, (byte[]) value.value);
            break;
        case Types.DATE:
            if (value.value == null) {
                ps.setNull(rank, Types.DATE);
                break;
            }
            ps.setDate(rank, (Date) value.value);
            break;
        case Types.TIMESTAMP:
            if (value.value == null) {
                ps.setNull(rank, Types.TIMESTAMP);
                break;
            }
            ps.setTimestamp(rank, (Timestamp) value.value);
            break;
        case Types.CLOB:
            if (value.value == null) {
                ps.setNull(rank, Types.CLOB);
                break;
            }
            ps.setClob(rank, (Reader) value.value);
            break;
        case Types.BLOB:
            if (value.value == null) {
                ps.setNull(rank, Types.BLOB);
                break;
            }
            ps.setBlob(rank, (InputStream) value.value);
            break;
        default:
            throw new WaarpDatabaseSqlException("Type not supported: " + value.type + " at " + rank);
        }
    } catch (ClassCastException e) {
        throw new WaarpDatabaseSqlException("Setting values casting error: " + value.type + " at " + rank, e);
    } catch (SQLException e) {
        DbSession.error(e);
        throw new WaarpDatabaseSqlException("Setting values in error: " + value.type + " at " + rank, e);
    }
}

From source file:org.schedoscope.metascope.tasks.repository.mysql.impl.TableEntityMySQLRepository.java

@Override
public void insertOrUpdate(Connection connection, TableEntity tableEntity) {
    String insertTableSql = "insert into table_entity ("
            + JDBCUtil.getDatabaseColumnsForClass(TableEntity.class) + ") values ("
            + JDBCUtil.getValuesCountForClass(TableEntity.class) + ") " + "on duplicate key update "
            + MySQLUtil.getOnDuplicateKeyString(TableEntity.class);
    PreparedStatement stmt = null;
    try {//from  w w  w. j av a  2 s  .c  o m
        stmt = connection.prepareStatement(insertTableSql);
        stmt.setString(1, tableEntity.getFqdn());
        stmt.setString(2, tableEntity.getTableName());
        stmt.setString(3, tableEntity.getDatabaseName());
        stmt.setString(4, tableEntity.getUrlPathPrefix());
        stmt.setBoolean(5, tableEntity.isExternalTable());
        stmt.setString(6, tableEntity.getTableDescription());
        stmt.setString(7, tableEntity.getStorageFormat());
        stmt.setString(8, tableEntity.getInputFormat());
        stmt.setString(9, tableEntity.getOutputFormat());
        stmt.setBoolean(10, tableEntity.isMaterializeOnce());
        stmt.setLong(11, tableEntity.getCreatedAt());
        stmt.setString(12, tableEntity.getStatus());
        stmt.setString(13, tableEntity.getTableOwner());
        stmt.setString(14, tableEntity.getDataPath());
        stmt.setLong(15, tableEntity.getDataSize());
        stmt.setString(16, tableEntity.getPermissions());
        stmt.setLong(17, tableEntity.getRowcount());
        stmt.setString(18, tableEntity.getTransformationType());
        stmt.setString(19, tableEntity.getLastData());
        stmt.setString(20, tableEntity.getTimestampField());
        stmt.setString(21, tableEntity.getTimestampFieldFormat());
        stmt.setLong(22, tableEntity.getLastChange());
        stmt.setLong(23, tableEntity.getLastPartitionCreated());
        stmt.setLong(24, tableEntity.getLastSchemaChange());
        stmt.setLong(25, tableEntity.getLastTransformation());
        stmt.execute();
    } catch (SQLException e) {
        LOG.error("Could not save table", e);
    } finally {
        DbUtils.closeQuietly(stmt);
    }
}

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/*from w w w .  j a va  2 s  .  co 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;
}