Example usage for java.sql PreparedStatement setLong

List of usage examples for java.sql PreparedStatement setLong

Introduction

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

Prototype

void setLong(int parameterIndex, long x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java long value.

Usage

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

public void setInstanceTime(int playerObjId, int id, long time) {
    if (!_playerInstanceTimes.containsKey(playerObjId))
        restoreInstanceTimes(playerObjId);
    Connection con = null;//from w w  w .  j  ava  2  s . c om
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement = null;
        statement = con.prepareStatement(ADD_INSTANCE_TIME);
        statement.setInt(1, playerObjId);
        statement.setInt(2, id);
        statement.setLong(3, time);
        statement.setLong(4, time);
        statement.execute();
        statement.close();
        _playerInstanceTimes.get(playerObjId).put(id, time);
    } catch (Exception e) {
        _log.warn("Could not insert character instance time data: ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.mobilewallet.users.dao.UserDAO.java

public UserProfile userProfile(long userId) {
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;/*  w  w w  .  ja  va  2s.  c  o m*/
    UserProfile user = null;
    try {
        connection = dataSource.getConnection();
        pstmt = connection.prepareStatement(getUserInfoQuery);
        pstmt.setLong(1, userId);
        rs = pstmt.executeQuery();

        if (rs.next()) {
            user = new UserProfile();
            user.setName(rs.getString("u_name"));
            user.setDob(rs.getString("dob"));
            user.setMcode(rs.getString("u_mobile_code"));
            user.setMnumber(rs.getString("u_mobile_number"));
            user.setOccupation(rs.getString("u_occupation"));
            user.setIncome(rs.getString("u_income"));
            user.setGender(rs.getString("u_gender"));
            user.setFbId(rs.getString("u_fb_id"));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {

        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception ex) {

        }
        try {
            if (pstmt != null) {
                pstmt.close();
            }
        } catch (Exception ex) {

        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception ex) {

        }
    }
    return user;
}

From source file:com.mobilewallet.credits.dao.CreditsDAO.java

public int debitCount(long userId) {
    int count = 0;
    Connection connection = null;
    PreparedStatement cstmt = null;
    ResultSet rs = null;//  ww  w .ja  v  a2 s  .co  m
    try {
        connection = dataSource.getConnection();
        cstmt = connection.prepareStatement(debitCountQuery);
        cstmt.setLong(1, userId);
        rs = cstmt.executeQuery();
        if (rs.next()) {
            count = rs.getInt(1);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception ex) {

        }
        try {
            if (cstmt != null) {
                cstmt.close();
            }
        } catch (Exception ex) {

        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception ex) {

        }
    }
    return count;
}

From source file:db.migration.V2017_08_27_14_00__Import_Images_Into_Db.java

@Override
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    LOG.info("Importing images into db");
    List<ImageObject> images = jdbcTemplate.query(
            "select `" + FIELD_ID + "`, `" + FIELD_FILE_PATH + "` from `" + TABLE_NAME + "`",
            new ImageObjectRowMapper<>());
    for (ImageObject entry : images) {
        try {/*from  w  w w  .  j  a v  a2s  .  c  o  m*/
            File file = new File(entry.getFilePath());
            byte[] data = IOUtils.toByteArray(new FileInputStream(file));
            if (data != null) {
                ImageCategory category = getCategory(entry.getFilePath());
                jdbcTemplate.update(connection -> {
                    PreparedStatement ps = connection.prepareStatement(
                            String.format("update `%s` set `%s` = ?, `%s` = ?, `%s` = ? where `%s` = ?",
                                    TABLE_NAME, FIELD_CONTENT_LENGTH, FIELD_CONTENT, FIELD_CATEGORY, FIELD_ID),
                            new String[] { FIELD_CONTENT });
                    ps.setLong(1, 0L + data.length);
                    ps.setBytes(2, data);
                    ps.setString(3, category.name());
                    ps.setLong(4, entry.getId());
                    return ps;
                });
            }
        } catch (Exception e) {
            LOG.error(String.format("Unable to import file %s: %s", entry.getFilePath(), e.getMessage()));
        }
    }
    LOG.info("Done importing images into db");
}

From source file:com.mobilewallet.credits.dao.CreditsDAO.java

public int creditCount(long userId) {
    int count = 0;
    Connection connection = null;
    PreparedStatement cstmt = null;
    ResultSet rs = null;//from  w w  w.j a  v  a  2 s. com
    try {
        connection = dataSource.getConnection();
        cstmt = connection.prepareStatement(creditCountQuery);
        cstmt.setLong(1, userId);
        rs = cstmt.executeQuery();
        if (rs.next()) {
            count = rs.getInt(1);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception ex) {

        }
        try {
            if (cstmt != null) {
                cstmt.close();
            }
        } catch (Exception ex) {

        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception ex) {

        }
    }
    return count;
}

From source file:com.saasovation.common.port.adapter.persistence.eventsourcing.mysql.MySQLJDBCEventStore.java

private void appendEventStore(Connection aConnection, EventStreamId anIdentity, int anIndex,
        DomainEvent aDomainEvent) throws Exception {

    PreparedStatement statement = aConnection
            .prepareStatement("INSERT INTO tbl_es_event_store VALUES(?, ?, ?, ?, ?)");

    statement.setLong(1, 0);
    statement.setString(2, this.serializer().serialize(aDomainEvent));
    statement.setString(3, aDomainEvent.getClass().getName());
    statement.setString(4, anIdentity.streamName());
    statement.setInt(5, anIdentity.streamVersion() + anIndex);

    statement.executeUpdate();/*from  w w w .  j a  v a2  s  .  c o  m*/
}

From source file:com.flexive.core.storage.H2.H2SequencerStorage.java

/**
 * {@inheritDoc}//ww  w . j  a v  a 2s.co  m
 */
@Override
public void setSequencerId(String name, long newId) throws FxApplicationException {
    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = Database.getDbConnection();
        ps = con.prepareStatement("ALTER SEQUENCE " + H2_SEQ_PREFIX + name + " RESTART WITH ?");
        ps.setLong(1, newId + 1);
        ps.executeUpdate();
    } catch (SQLException exc) {
        throw new FxDbException(LOG, exc, "ex.db.sqlError", exc.getMessage());
    } finally {
        Database.closeObjects(H2SequencerStorage.class, con, ps);
    }
}

From source file:dk.netarkivet.harvester.datamodel.extendedfield.ExtendedFieldValueDBDAO.java

@Override
public void delete(long aExtendedfieldValueID) throws IOFailure {
    ArgumentNotValid.checkNotNull(aExtendedfieldValueID, "aExtendedfieldValueID");

    Connection c = HarvestDBConnection.get();
    PreparedStatement stm = null;
    try {//from w ww  . ja  v  a  2  s.co  m
        c.setAutoCommit(false);

        stm = c.prepareStatement("DELETE FROM extendedfieldvalue " + "WHERE extendedfieldvalue_id = ?");
        stm.setLong(1, aExtendedfieldValueID);
        stm.executeUpdate();

        c.commit();

    } catch (SQLException e) {
        String message = "SQL error deleting extendedfieldvalue for ID " + aExtendedfieldValueID + "\n"
                + ExceptionUtils.getSQLExceptionCause(e);
        log.warn(message, e);
    } finally {
        DBUtils.closeStatementIfOpen(stm);
        DBUtils.rollbackIfNeeded(c, "delete extendedfield value", aExtendedfieldValueID);
        HarvestDBConnection.release(c);
    }

}

From source file:com.ushahidi.swiftriver.core.api.dao.impl.JpaPlaceDao.java

/**
 * Populate the droplet places table.//from  ww  w .j  a  v  a  2  s  . c  o  m
 * 
 * @param drops
 */
private void insertDropletPlaces(List<Drop> drops) {

    // List of drop IDs in the drops list
    List<Long> dropIds = new ArrayList<Long>();
    // List of places in a drop
    Map<Long, Set<Long>> dropletPlacesMap = new HashMap<Long, Set<Long>>();
    for (Drop drop : drops) {

        if (drop.getPlaces() == null)
            continue;

        dropIds.add(drop.getId());

        for (Place place : drop.getPlaces()) {
            Set<Long> places = null;
            if (dropletPlacesMap.containsKey(drop.getId())) {
                places = dropletPlacesMap.get(drop.getId());
            } else {
                places = new HashSet<Long>();
                dropletPlacesMap.put(drop.getId(), places);
            }

            places.add(place.getId());
        }
    }

    // Find droplet places that already exist in the db
    String sql = "SELECT droplet_id, place_id FROM droplets_places WHERE droplet_id in (:ids)";

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("ids", dropIds);

    List<Map<String, Object>> results = this.namedJdbcTemplate.queryForList(sql, params);

    // Remove already existing droplet_places from our Set
    for (Map<String, Object> result : results) {
        long dropletId = ((Number) result.get("droplet_id")).longValue();
        long placeId = ((Number) result.get("place_id")).longValue();

        Set<Long> placeSet = dropletPlacesMap.get(dropletId);
        if (placeSet != null) {
            placeSet.remove(placeId);
        }
    }

    // Insert the remaining items in the set into the db
    sql = "INSERT INTO droplets_places (droplet_id, place_id) VALUES (?,?)";

    final List<long[]> dropletPlacesList = new ArrayList<long[]>();
    for (Long dropletId : dropletPlacesMap.keySet()) {
        for (Long placeId : dropletPlacesMap.get(dropletId)) {
            long[] dropletPlace = { dropletId, placeId };
            dropletPlacesList.add(dropletPlace);
        }
    }
    jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            long[] dropletPlace = dropletPlacesList.get(i);
            ps.setLong(1, dropletPlace[0]);
            ps.setLong(2, dropletPlace[1]);
        }

        public int getBatchSize() {
            return dropletPlacesList.size();
        }
    });
}

From source file:com.exploringspatial.dao.impl.ConflictDaoImpl.java

@Override
public void batchUpdate(List<Conflict> conflicts) {

    jdbcTemplate.batchUpdate(batchUpdateSql, new BatchPreparedStatementSetter() {

        @Override/*from   w  w  w .  j av a 2s  .  c  o  m*/
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            final DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            final Conflict instance = conflicts.get(i);
            ps.setLong(1, instance.getGwno());
            ps.setString(2, instance.getEventIdCountry());
            ps.setLong(3, instance.getEventPk());
            ps.setString(4, df.format(instance.getEventDate()));
            ps.setLong(5, instance.getYear());
            ps.setLong(6, instance.getTimePrecision());
            ps.setString(7, instance.getEventType());
            ps.setString(8, instance.getActor1());
            ps.setString(9, instance.getAllyActor1());
            ps.setLong(10, instance.getInter1());
            ps.setString(11, instance.getActor2());
            ps.setString(12, instance.getAllyActor2());
            ps.setLong(13, instance.getInter2());
            ps.setLong(14, instance.getInteraction());
            ps.setString(15, instance.getCountry());
            ps.setString(16, instance.getAdmin1());
            ps.setString(17, instance.getAdmin2());
            ps.setString(18, instance.getAdmin3());
            ps.setString(19, instance.getLocation());
            ps.setDouble(20, instance.getLatitude());
            ps.setDouble(21, instance.getLongitude());
            ps.setLong(22, instance.getGwno());
            ps.setString(23, instance.getSource());
            ps.setString(24, instance.getNotes());
            ps.setLong(25, instance.getFatalities());
        }

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

}