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:net.solarnetwork.node.dao.jdbc.power.JdbcPowerDatumDao.java

@Override
protected void setStoreStatementValues(PowerDatum datum, PreparedStatement ps) throws SQLException {
    int col = 1;//www. j  av  a  2  s.co m
    ps.setTimestamp(col++, new java.sql.Timestamp(
            datum.getCreated() == null ? System.currentTimeMillis() : datum.getCreated().getTime()));
    ps.setString(col++, datum.getSourceId() == null ? "" : datum.getSourceId());
    if (datum.getLocationId() == null) {
        ps.setNull(col++, Types.BIGINT);
    } else {
        ps.setLong(col++, datum.getLocationId());
    }
    if (datum.getWatts() == null) {
        ps.setNull(col++, Types.INTEGER);
    } else {
        ps.setInt(col++, datum.getWatts());
    }
    if (datum.getBatteryVolts() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getBatteryVolts());
    }
    if (datum.getBatteryAmpHours() == null) {
        ps.setNull(col++, Types.DOUBLE);
    } else {
        ps.setDouble(col++, datum.getBatteryAmpHours());
    }
    if (datum.getDcOutputVolts() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getDcOutputVolts());
    }
    if (datum.getDcOutputAmps() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getDcOutputAmps());
    }
    if (datum.getAcOutputVolts() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getAcOutputVolts());
    }
    if (datum.getAcOutputAmps() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getAcOutputAmps());
    }
    if (datum.getWattHourReading() == null) {
        ps.setNull(col++, Types.BIGINT);
    } else {
        ps.setLong(col++, datum.getWattHourReading());
    }
    if (datum.getAmpHourReading() == null) {
        ps.setNull(col++, Types.DOUBLE);
    } else {
        ps.setDouble(col++, datum.getAmpHourReading());
    }
}

From source file:com.flexive.core.storage.genericSQL.GenericBinarySQLOutputStream.java

/**
 * {@inheritDoc}/*  w  w  w. j a v a2  s.  c om*/
 */
@Override
public void run() {
    PreparedStatement ps = null;
    Connection con = null;
    try {
        con = Database.getNonTXDataSource(divisionId).getConnection();
        ps = con.prepareStatement("INSERT INTO " + DatabaseConst.TBL_BINARY_TRANSIT
                + " (BKEY,MIMETYPE,FBLOB,TFER_DONE,EXPIRE) VALUES(?,?,?,?,?)");
        ps.setString(1, handle);
        ps.setString(2, mimeType);
        ps.setBinaryStream(3, pipe, (int) expectedSize);
        ps.setBoolean(4, false);
        ps.setLong(5, System.currentTimeMillis() + ttl);
        long time = System.currentTimeMillis();
        ps.executeUpdate();
        if (LOG.isDebugEnabled())
            LOG.debug("Stored " + count + "/" + expectedSize + " bytes in "
                    + (System.currentTimeMillis() - time) + "[ms] in DB");
        try {
            pipe.close();
        } catch (IOException e) {
            LOG.error("IO error closing pipe: " + e.getMessage(), e);
        }
    } catch (SQLException e) {
        LOG.error("SQL error storing binary: " + e.getMessage(), e);
    } finally {
        Database.closeObjects(GenericBinarySQLOutputStream.class, con, ps);
    }
}

From source file:com.flexive.ejb.beans.LanguageBean.java

/**
 * Check if the given language is referenced from a table
 *
 * @param con      an open and valid connection
 * @param language the language to check
 * @return if the language is in use//from ww w  .j av  a  2 s . com
 * @throws FxApplicationException on errors
 * @throws SQLException           on errors
 */
private boolean hasUsages(Connection con, FxLanguage language) throws FxApplicationException, SQLException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Checking for uses of language " + language.getIso2digit());
    }
    PreparedStatement ps = null;
    try {
        ResultSet rs;
        for (String[] check : LANG_USAGE) {
            if (ps != null)
                ps.close();
            ps = con.prepareStatement("SELECT COUNT(*) FROM " + check[0] + " WHERE " + check[1] + "=?");
            ps.setLong(1, language.getId());
            rs = ps.executeQuery();
            if (rs != null && rs.next()) {
                if (rs.getLong(1) > 0) {
                    LOG.info("Language [" + language.getIso2digit() + "] has [" + rs.getLong(1)
                            + "] usages in table " + check[0] + ", column " + check[1]);
                    return true;
                }
            }
        }
    } finally {
        if (ps != null)
            ps.close();
    }
    return false;
}

From source file:iddb.runtime.db.model.dao.impl.mysql.PenaltyDAOImpl.java

@Override
public void delete(Penalty penalty) {
    String sql = "delete from penalty where id = ?";
    Connection conn = null;//from   w  ww .jav a2 s.c om
    try {
        conn = ConnectionFactory.getMasterConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        st.setLong(1, penalty.getKey());
        int r = st.executeUpdate();
        logger.debug("{} penalty removed", r);
    } catch (SQLException e) {
        logger.error("delete: {}", e);
    } catch (IOException e) {
        logger.error("delete: {}", e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
}

From source file:info.raack.appliancedetection.evaluation.data.JDBCDatabase.java

@Override
public void saveSimulationGroup(final SimulationGroup group) {
    KeyHolder keyHolder = new GeneratedKeyHolder();

    jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into simulation_groups (start_time, duration, num_appliances, on_concurrency, labels_per_appliance) values (?, ?, ?, ?, ?)",
                    new String[] { "id" });
            ps.setLong(1, group.getStartTime().getTime());
            ps.setInt(2, group.getDurationInSeconds());
            ps.setInt(3, group.getNumAppliances());
            ps.setInt(4, group.getOnConcurrency());
            ps.setInt(5, group.getLabelsPerOnOff());

            return ps;
        }/*from w w w  . j a  v  a2 s  . com*/
    }, keyHolder);

    group.setId(keyHolder.getKey().intValue());
}

From source file:iddb.runtime.db.model.dao.impl.mysql.PenaltyDAOImpl.java

@Override
public void deletePlayerPenalty(Long player, Integer type) {
    String sql = "delete from penalty where playerid = ? and type = ?";
    Connection conn = null;// www.  j  a  va 2  s  . co m
    try {
        conn = ConnectionFactory.getMasterConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        st.setLong(1, player);
        st.setInt(2, type);
        int r = st.executeUpdate();
        logger.debug("{} penalties removed", r);
    } catch (SQLException e) {
        logger.error("deletePlayerPenalty: {}", e);
    } catch (IOException e) {
        logger.error("deletePlayerPenalty: {}", e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
}

From source file:iddb.runtime.db.model.dao.impl.mysql.PenaltyDAOImpl.java

@Override
public List<Penalty> findByPlayer(Long player) {
    String sql = "select * from penalty where playerid = ?";
    Connection conn = null;/*from   w  w  w  .  j  av  a  2  s. c om*/
    List<Penalty> list = new ArrayList<Penalty>();
    try {
        conn = ConnectionFactory.getSecondaryConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        st.setLong(1, player);
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            Penalty penalty = new Penalty();
            loadPenalty(penalty, rs);
            list.add(penalty);
        }
    } catch (SQLException e) {
        logger.error("findByPlayer: {}", e);
    } catch (IOException e) {
        logger.error("findByPlayer: {}", e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
    return list;
}

From source file:iddb.runtime.db.model.dao.impl.mysql.PenaltyDAOImpl.java

@Override
public List<Penalty> findPendingPenalties(Long serverId) {
    String sql = "select p.* from penalty p, player pa, penalty_history h where p.id = h.penaltyid and p.playerid = pa.id and p.synced = 0 and h.status = 0 and pa.serverid = ?";
    Connection conn = null;//  w ww  .  ja v a2  s . c  om
    List<Penalty> list = new ArrayList<Penalty>();
    try {
        conn = ConnectionFactory.getSecondaryConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        st.setLong(1, serverId);
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            Penalty penalty = new Penalty();
            loadPenalty(penalty, rs);
            list.add(penalty);
        }
    } catch (SQLException e) {
        logger.error("findPendingPenalties: {}", e);
    } catch (IOException e) {
        logger.error("findPendingPenalties: {}", e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
    return list;
}

From source file:iddb.runtime.db.model.dao.impl.mysql.PenaltyDAOImpl.java

@Override
public Penalty get(Long key) throws EntityDoesNotExistsException {
    String sql = "select * from penalty where id = ?";
    Connection conn = null;/*w  w  w.j av  a  2 s  .  co m*/
    Penalty penalty = null;
    try {
        conn = ConnectionFactory.getSecondaryConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        st.setLong(1, key);
        ResultSet rs = st.executeQuery();
        if (rs.next()) {
            penalty = new Penalty();
            loadPenalty(penalty, rs);
        } else {
            throw new EntityDoesNotExistsException("Penalty with id %s was not found", key.toString());
        }
    } catch (SQLException e) {
        logger.error("get: {}", e);
    } catch (IOException e) {
        logger.error("get: {}", e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
    return penalty;
}

From source file:iddb.runtime.db.model.dao.impl.mysql.PenaltyDAOImpl.java

@Override
public List<Penalty> findByPlayer(Long player, int limit) {
    String sql = "select * from penalty where playerid = ? limit ?";
    List<Penalty> list = new ArrayList<Penalty>();
    Connection conn = null;/*from   ww w .j  a  v a  2s.co  m*/
    try {
        conn = ConnectionFactory.getSecondaryConnection();
        PreparedStatement st = conn.prepareStatement(sql);
        st.setLong(1, player);
        st.setInt(2, limit);
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            Penalty penalty = new Penalty();
            loadPenalty(penalty, rs);
            list.add(penalty);
        }
    } catch (SQLException e) {
        logger.error("findByPlayer: {}", e);
    } catch (IOException e) {
        logger.error("findByPlayer: {}", e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
    return list;
}