Example usage for java.sql PreparedStatement setInt

List of usage examples for java.sql PreparedStatement setInt

Introduction

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

Prototype

void setInt(int parameterIndex, int x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java int value.

Usage

From source file:alfio.manager.UploadedResourceManager.java

public int saveResource(int organizationId, UploadBase64FileModification file) {
    if (hasResource(organizationId, file.getName())) {
        uploadedResourceRepository.delete(organizationId, file.getName());
    }/*  w w  w. j  av a2 s.c  om*/
    LobHandler lobHandler = new DefaultLobHandler();
    return jdbc.getJdbcOperations().execute(
            uploadedResourceRepository.uploadTemplate(organizationId, file.getName()),
            new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                @Override
                protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                    ps.setInt(2, organizationId);
                    setFileValues(ps, lobCreator, file, 2);
                }
            });
}

From source file:com.stratelia.webactiv.util.DBUtil.java

/**
 * Centralization in order to sets the parameters on a prepare statement.
 * @param preparedStatement//from  www  . j  av  a 2 s .c o  m
 * @param parameters
 * @throws SQLException
 */
public static <O> void setParameters(PreparedStatement preparedStatement, Collection<O> parameters)
        throws SQLException {
    int paramIndex = 1;
    for (Object parameter : parameters) {
        if (parameter == null) {
            preparedStatement.setObject(paramIndex, null);
        } else if (parameter instanceof String) {
            preparedStatement.setString(paramIndex, (String) parameter);
        } else if (parameter instanceof Enum) {
            preparedStatement.setString(paramIndex, ((Enum) parameter).name());
        } else if (parameter instanceof Integer) {
            preparedStatement.setInt(paramIndex, (Integer) parameter);
        } else if (parameter instanceof Long) {
            preparedStatement.setLong(paramIndex, (Long) parameter);
        } else if (parameter instanceof Timestamp) {
            preparedStatement.setTimestamp(paramIndex, (Timestamp) parameter);
        } else if (parameter instanceof Date) {
            preparedStatement.setDate(paramIndex, new java.sql.Date(((Date) parameter).getTime()));
        } else if (parameter instanceof UserDetail) {
            preparedStatement.setString(paramIndex, ((UserDetail) parameter).getId());
        } else {
            throw new IllegalArgumentException("SQL parameter type not handled: " + parameter.getClass());
        }
        paramIndex++;
    }
}

From source file:fll.web.playoff.Playoff.java

/**
 * Get the max run number for a given playoff division.
 * This run number specifies the winner of the playoff division.
 * /* w w w  . j  a  v a  2s  .com*/
 * @return the run max run number or -1 if not found
 */
public static int getMaxRunNumber(final Connection connection, final int tournament, final String division)
        throws SQLException {
    PreparedStatement prep = null;
    ResultSet rs = null;
    try {
        prep = connection.prepareStatement(
                "SELECT MAX(run_number) FROM PlayoffData WHERE event_division = ? AND Tournament = ?");
        prep.setString(1, division);
        prep.setInt(2, tournament);
        rs = prep.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        } else {
            return -1;
        }
    } finally {
        SQLFunctions.close(rs);
        SQLFunctions.close(prep);
    }
}

From source file:com.l2jfree.gameserver.model.ShortCuts.java

public synchronized void deleteShortCut(int slot, int page) {
    L2ShortCut old = _shortCuts.remove(slot + page * 12);
    if (old == null)
        return;//from   ww  w  . j a  v a2 s. co  m

    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);

        PreparedStatement statement = con.prepareStatement(
                "DELETE FROM character_shortcuts WHERE charId=? AND slot=? AND page=? AND class_index=?");
        statement.setInt(1, _owner.getObjectId());
        statement.setInt(2, old.getSlot());
        statement.setInt(3, old.getPage());
        statement.setInt(4, _owner.getClassIndex());
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }

    if (old.getType() == L2ShortCut.TYPE_ITEM) {
        L2ItemInstance item = _owner.getInventory().getItemByObjectId(old.getId());

        if (item != null && item.getItemType() == L2EtcItemType.SHOT)
            _owner.getShots().removeAutoSoulShot(item.getItemId());
    }

    _owner.sendPacket(new ShortCutInit(_owner));

    for (int shotId : _owner.getShots().getAutoSoulShots())
        _owner.sendPacket(new ExAutoSoulShot(shotId, 1));
}

From source file:alfio.manager.UploadedResourceManager.java

public int saveResource(int organizationId, int eventId, UploadBase64FileModification file) {
    if (hasResource(organizationId, eventId, file.getName())) {
        uploadedResourceRepository.delete(organizationId, eventId, file.getName());
    }//from  w  w  w.  j  a  v  a 2s .  c  o m
    LobHandler lobHandler = new DefaultLobHandler();
    return jdbc.getJdbcOperations().execute(
            uploadedResourceRepository.uploadTemplate(organizationId, eventId, file.getName()),
            new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                @Override
                protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                    ps.setInt(2, organizationId);
                    ps.setInt(3, eventId);
                    setFileValues(ps, lobCreator, file, 3);
                }
            });

}

From source file:oobbit.orm.Comments.java

/**
 * Updates the given comment's content.//  w w  w .jav a  2s.c o  m
 *
 * @param comment
 *
 * @return ID to the link in which the comment is posted to
 *
 * @throws SQLException PreparedStatement failed to execute
 */
public int update(Comment comment) throws SQLException {
    PreparedStatement statement = getConnection().prepareStatement(
            "UPDATE `oobbit`.`comments` SET `content` = ? WHERE `comments`.`comment_id` = ?;");
    statement.setString(1, comment.getContent());
    statement.setInt(2, comment.getId());

    statement.executeUpdate();

    return comment.getLinkId();
}

From source file:org.openmrs.module.openhmis.plm.db.DatabaseListProvider.java

/**
 * Adds a new item to the list./*from  w w  w .j  a  v  a 2s. c  o m*/
 * @param item The item to add.
 * @should add the item to the database
 * @should correctly update the index of the other items in the table
 * @should throw PersistentListException when the command returns 0 rows updated
 * @should roll back the transaction when the operation fails
 * @shold commit the transaction if no operations fail
 */
@Override
public void add(final PersistentListItemModel item) {
    Session session = sessionFactory.getCurrentSession();
    Transaction trans = null;

    try {
        // Start transaction
        trans = session.beginTransaction();

        // Update all items >= index to be their current index + 1
        // TODO: Can this type of query be handled by Hibernate without needing to use hardcoded SQL?
        session.doWork(new Work() {
            public void execute(Connection connection) {
                try {
                    PreparedStatement cmd = connection.prepareStatement(ADD_SQL);
                    cmd.setInt(1, item.getListId());
                    cmd.setInt(2, item.getItemOrder());

                    cmd.executeUpdate();
                } catch (SQLException sex) {
                    throw new PersistentListException(sex);
                }
            }
        });

        // Insert item with index
        session.save(item);
        session.flush();

        // Commit transaction
        trans.commit();
    } catch (Exception ex) {
        log.debug("The list add operation failed.  Rolling back transaction...");
        trans.rollback();
        log.debug("Transaction rolled back.");

        throw new PersistentListException("An exception occurred while attempting to add the item to the list.",
                ex);
    } finally {
        session.close();
    }
}

From source file:com.l2jfree.gameserver.model.MacroList.java

/**
 * @param shortcut//  w w  w.j  a  v  a 2 s  .  c o m
 */
private void deleteMacroFromDb(L2Macro macro) {
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);

        PreparedStatement statement = con
                .prepareStatement("DELETE FROM character_macroses WHERE charId=? AND id=?");
        statement.setInt(1, _owner.getObjectId());
        statement.setInt(2, macro.id);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("could not delete macro:", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:fll.web.playoff.Playoff.java

/**
 * Get the performance score for the given team, tournament and run number
 * //  w  w w  .j  a  v  a2s.c  om
 * @throws SQLException on a database error
 * @throws IllegalArgumentException if no score exists
 */
public static double getPerformanceScore(final Connection connection, final int tournament, final Team team,
        final int runNumber) throws SQLException, IllegalArgumentException {
    if (null == team) {
        throw new IllegalArgumentException("Cannot get score for null team");
    } else {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            stmt = connection.prepareStatement("SELECT ComputedTotal FROM Performance WHERE TeamNumber = ?"
                    + " AND Tournament = ?" + " AND RunNumber = ?");
            stmt.setInt(1, team.getTeamNumber());
            stmt.setInt(2, tournament);
            stmt.setInt(3, runNumber);
            rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getDouble(1);
            } else {
                throw new IllegalArgumentException("No score exists for tournament: " + tournament
                        + " teamNumber: " + team.getTeamNumber() + " runNumber: " + runNumber);
            }
        } finally {
            SQLFunctions.close(rs);
            SQLFunctions.close(stmt);
        }
    }
}

From source file:oobbit.orm.Comments.java

public void add(Comment comment) throws SQLException {
    PreparedStatement statement = getConnection().prepareStatement(
            "INSERT INTO `oobbit`.`comments` (`comment_id`, `link_id`, `creator`, `content`, `create_time`) VALUES (NULL, ?, ?, ?, CURRENT_TIMESTAMP);");
    statement.setInt(1, comment.getLinkId());
    statement.setInt(2, comment.getCreatorId());
    statement.setString(3, comment.getContent());

    statement.executeUpdate();/*from   ww w .  ja v  a  2  s . c  o  m*/
    statement.close();
}