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:dk.netarkivet.common.utils.DBUtils.java

/**
 * Insert an Integer in prepared statement.
 * @param s a prepared statement/*w w w  . j a  va  2s  . c o m*/
 * @param i the index of the statement, where the Integer should be inserted
 * @param value The Integer to insert (maybe null)
 * @throws SQLException If i does not correspond to a
 * parameter marker in the PreparedStatement, or a database access error
 * occurs or this method is called on a closed PreparedStatement
 */
public static void setIntegerMaybeNull(PreparedStatement s, int i, Integer value) throws SQLException {
    ArgumentNotValid.checkNotNull(s, "PreparedStatement s");

    if (value != null) {
        s.setInt(i, value);
    } else {
        s.setNull(i, Types.INTEGER);
    }
}

From source file:com.github.ffremont.writers.PersonDaoWriter.java

@Override
public void write(List<? extends Person> list) throws Exception {

    jdbcTemplate.batchUpdate(INSERTS, new BatchPreparedStatementSetter() {
        @Override//from   w w  w. j  a v a  2 s .c  om
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            Person person = list.get(i);
            ps.setInt(1, person.getId());
            ps.setString(2, person.getNom());
            ps.setString(3, person.getPrenom());
            ps.setString(4, person.getCivilite());
        }

        @Override
        public int getBatchSize() {
            return list.size();
        }

    });

}

From source file:biblivre3.cataloging.bibliographic.IndexDAO.java

public final boolean delete(final IndexTable table, final IndexDTO index) {
    Connection con = null;//from   w w w  . ja v  a2 s.c o m
    try {
        con = getDataSource().getConnection();
        final String sql = " DELETE FROM " + table.getTableName() + " WHERE record_serial = ?;";

        final PreparedStatement pst = con.prepareStatement(sql);
        pst.setInt(1, index.getRecordSerial());

        return pst.executeUpdate() > 0;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new ExceptionUser("ERROR_BIBLIO_DAO_EXCEPTION");
    } finally {
        closeConnection(con);
    }
}

From source file:net.mms_projects.copy_it.api.http.pages.v1.ClipboardGet.java

public FullHttpResponse onGetRequest(HttpRequest request, Database database, HeaderVerifier headerVerifier)
        throws Exception {
    if (!headerVerifier.getConsumerScope().canRead() || !headerVerifier.getUserScope().canRead())
        throw new ErrorException(NO_READ_PERMISSION);
    PreparedStatement statement = database.getConnection().prepareStatement(SELECT_CONTENT);
    statement.setInt(1, headerVerifier.getUserId());
    ResultSet result = statement.executeQuery();
    if (result.first()) {
        final JSONObject json = new JSONObject();
        json.put(CONTENT, result.getString(DATA));
        json.put(LAST_UPDATED, result.getInt(LAST_UPDATED));
        result.close();// w  w  w . j  a va2  s.c  o m
        return new DefaultFullHttpResponse(request.getProtocolVersion(), OK,
                Unpooled.copiedBuffer(json.toString(), CharsetUtil.UTF_8));
    }
    result.close();
    throw new NoContentException();
}

From source file:edu.umd.cs.marmoset.modelClasses.CodeMetrics.java

private int putValues(PreparedStatement stmt, int index) throws SQLException {
    stmt.setInt(index++, getTestRunPK());
    stmt.setString(index++, getMd5sumSourcefiles());
    stmt.setString(index++, getMd5sumClassfiles());
    stmt.setInt(index++, getCodeSegmentSize());
    return index;
}

From source file:com.l2jfree.gameserver.instancemanager.ContestableHideoutGuardManager.java

public final void load() {
    Connection con = null;/*from   w  w  w  .  jav  a2 s .co  m*/
    FastList<L2Spawn> guards = new FastList<L2Spawn>(50);
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement ps = con.prepareStatement(LOAD_SIEGE_GUARDS);
        ps.setInt(1, _hideout.getId());
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            L2Spawn s = new L2Spawn(NpcTable.getInstance().getTemplate(rs.getInt("npcId")));
            s.setId(rs.getInt("id"));
            s.setAmount(1);
            s.setLocx(rs.getInt("x"));
            s.setLocy(rs.getInt("y"));
            s.setLocz(rs.getInt("z"));
            s.setHeading(rs.getInt("heading"));
            s.setRespawnDelay(rs.getInt("respawnDelay"));
            s.setLocation(0);
            guards.add(s);
        }
        _guardSpawn = guards.toArray(new L2Spawn[guards.size()]);
    } catch (Exception e) {
        _log.error("Failed loading " + _hideout.getName() + "'s siege guards!", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:mercury.DigitalMediaDAO.java

public final DigitalMediaDTO getDigitalMedia(int id, String fileName) {
    Connection con = null;//from   www. j a v  a 2s .  c o  m
    byte[] blob = null;
    DigitalMediaDTO dto = new DigitalMediaDTO();
    try {
        con = getDataSource().getConnection();
        String sql = " SELECT file, file_name, mime_type FROM digital_media " + " WHERE id = ? "
                + " AND file_name = ? ;";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setInt(1, id);
        pst.setString(2, fileName);
        con.setAutoCommit(false);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            blob = rs.getBytes(1);
            if (blob == null) {
                return null;
            }
            dto.setIn(new ByteArrayInputStream(blob));
            dto.setLength((int) blob.length);
            dto.setFileName(rs.getString(2));
            dto.setMimeType(rs.getString(3));
        }
        return dto;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new DAOException(e.getMessage());
    } finally {
        closeConnection(con);
    }
}

From source file:com.ccoe.build.dal.RawDataJDBCTemplate.java

public int create(final Plugin plugin, final int sessionID, final int projectID) {
    final String SQL = "insert into RBT_RAW_DATA (plugin_id, session_id, "
            + "project_id, duration, event_time, plugin_key) values (?, ?, ?, ?, ?, ?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplateObject.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(SQL, new String[] { "id" });
            ps.setInt(1, plugin.getId());
            ps.setInt(2, sessionID);// w  ww .  j a  va  2s .  co  m
            ps.setInt(3, projectID);
            ps.setLong(4, plugin.getDuration());
            ps.setTimestamp(5, new java.sql.Timestamp(plugin.getStartTime().getTime()));
            ps.setString(6, plugin.getGroupId() + ":" + plugin.getArtifactId());

            return ps;
        }
    }, keyHolder);

    return keyHolder.getKey().intValue();
}

From source file:net.mms_projects.copy_it.api.http.pages.v1.ClipboardUpdate.java

public void dispatchNotification(Database database, int user_id) throws SQLException {
    PreparedStatement statement = database.getConnection().prepareStatement(SELECT_GCM_TOKENS);
    statement.setInt(1, user_id);
    ResultSet resultSet = statement.executeQuery();
    if (resultSet.first()) {
        GCMRunnable gcm = new GCMRunnable();
        do {/* ww  w .  j  a  va 2s.  c o m*/
            gcm.addRegistrationId(resultSet.getString(GCM_TOKEN));
        } while (resultSet.next());
        gcm.setData("action", "content-updated");
        postProcess(gcm);
    }
    resultSet.close();
}

From source file:com.bounswe2015group5.xplore.AllCommentsServlet.java

private JSONArray getAllComments(String contribId) {
    try {/*  www .  ja v  a  2 s . c  o  m*/
        JSONArray result = new JSONArray();
        DBConnection conn = new DBConnection();
        String sql = "select " + "Comment.Content, " + "Comment.Date, " + "User2.Name, " + "User2.Surname "
                + "from Comment " + "inner join User2 " + "on Comment.UserID = User2.ID "
                + "where Comment.ContribID = ?;";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setInt(1, Integer.parseInt(contribId));
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            JSONObject row = new JSONObject();
            row.put("content", rs.getString("Content"));
            row.put("date", rs.getString("Date"));
            row.put("name", rs.getString("Name"));
            row.put("surname", rs.getString("Surname"));
            result.put(row);
        }
        rs.close();
        stmt.close();
        conn.close();
        return result;
    } catch (SQLException | ClassNotFoundException | NumberFormatException | JSONException e) {
        return new JSONArray();
    }
}