List of usage examples for java.sql PreparedStatement setDouble
void setDouble(int parameterIndex, double x) throws SQLException;
double
value. From source file:com.ibm.bluemix.samples.PostgreSQLClient.java
/** * Insert text into PostgreSQL//from w w w . ja va2 s.c om * * param posts List of Strings of text to insert * * @return number of rows affected * @throws Exception * @throws Exception */ public int addTrack(String notesID, String liquidID, String name, String techDomain, String techOther, String eventType, String eventOther, String registerDate, String completeDate, String status, String isFirst, String isSecond, double winDollar, double winHour, double winPoint) throws Exception { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("INSERT INTO track ( "); sqlBuilder.append("NotesID "); sqlBuilder.append(",LiquidID "); sqlBuilder.append(",Name "); sqlBuilder.append(",TechDomain "); sqlBuilder.append(",TechOther "); sqlBuilder.append(",EventType "); sqlBuilder.append(",EventOther "); sqlBuilder.append(",RegisterDate "); sqlBuilder.append(",CompleteDate "); sqlBuilder.append(",Status "); sqlBuilder.append(",IsFirst "); sqlBuilder.append(",IsSecond "); sqlBuilder.append(",WinDollar "); sqlBuilder.append(",WinHour "); sqlBuilder.append(",WinPoint "); sqlBuilder.append(",AddDate "); sqlBuilder.append(") VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,now() )"); Connection connection = null; PreparedStatement statement = null; try { connection = getConnection(); statement = connection.prepareStatement(sqlBuilder.toString()); statement.setString(1, notesID); statement.setString(2, liquidID); statement.setString(3, name); statement.setString(4, techDomain); statement.setString(5, techOther); statement.setString(6, eventType); statement.setString(7, eventOther); statement.setString(8, registerDate); statement.setString(9, completeDate); statement.setString(10, status); statement.setString(11, isFirst); statement.setString(12, isSecond); statement.setDouble(13, winDollar); statement.setDouble(14, winHour); statement.setDouble(15, winPoint); return statement.executeUpdate(); } catch (SQLException e) { SQLException next = e.getNextException(); if (next != null) { throw next; } throw e; } finally { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } }
From source file:lineage2.gameserver.model.instances.PetInstance.java
/** * Method store./*from ww w .j av a 2 s . c om*/ */ public void store() { if ((getControlItemObjId() == 0) || (_exp == 0)) { return; } Connection con = null; PreparedStatement statement = null; try { con = DatabaseFactory.getInstance().getConnection(); String req; if (!isRespawned()) { req = "INSERT INTO pets (name,level,curHp,curMp,exp,sp,fed,objId,item_obj_id) VALUES (?,?,?,?,?,?,?,?,?)"; } else { req = "UPDATE pets SET name=?,level=?,curHp=?,curMp=?,exp=?,sp=?,fed=?,objId=? WHERE item_obj_id = ?"; } statement = con.prepareStatement(req); statement.setString(1, getName().equalsIgnoreCase(getTemplate().name) ? "" : getName()); statement.setInt(2, _level); statement.setDouble(3, getCurrentHp()); statement.setDouble(4, getCurrentMp()); statement.setLong(5, _exp); statement.setLong(6, _sp); statement.setInt(7, _curFed); statement.setInt(8, getObjectId()); statement.setInt(9, _controlItemObjId); statement.executeUpdate(); } catch (Exception e) { _log.error("Could not store pet data!", e); } finally { DbUtils.closeQuietly(con, statement); } _respawned = true; }
From source file:com.l2jfree.gameserver.instancemanager.hellbound.HellboundManager.java
private void loadWarpgates() { Connection con = null;//w w w .ja v a 2 s. c om try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("SELECT * FROM hellbounds WHERE variable=?"); statement.setString(1, "warpgates_energy"); ResultSet rset = statement.executeQuery(); if (rset.next()) _warpgateEnergy = rset.getInt("value"); else _warpgateEnergy = 0; rset.close(); statement.setString(1, "warpgatesLastcheck"); rset = statement.executeQuery(); if (rset.next()) { double lastCheck = rset.getDouble("value"); if (System.currentTimeMillis() >= lastCheck + (24 * 60 * 60 * 1000)) { subWarpgateEnergy(10000); try { statement = con.prepareStatement("REPLACE INTO hellbounds VALUES (?,?)"); statement.setString(1, "warpgatesLastcheck"); statement.setDouble(2, System.currentTimeMillis()); statement.execute(); statement.close(); } catch (Exception e) { _log.error("Could not update last warpgates check.", e); return; } } } else _warpgateEnergy = 0; rset.close(); } catch (Exception e) { _log.error("Cannot get warpgates last check.", e); } finally { L2DatabaseFactory.close(con); } }
From source file:com.kylinolap.rest.service.QueryService.java
/** * @param preparedState//w w w . j a va 2 s. c om * @param param * @throws SQLException */ private void setParam(PreparedStatement preparedState, int index, StateParam param) throws SQLException { boolean isNull = (null == param.getValue()); Class<?> clazz = Object.class; try { clazz = Class.forName(param.getClassName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } Rep rep = Rep.of(clazz); switch (rep) { case PRIMITIVE_CHAR: case CHARACTER: case STRING: preparedState.setString(index, isNull ? null : String.valueOf(param.getValue())); break; case PRIMITIVE_INT: case INTEGER: preparedState.setInt(index, isNull ? null : Integer.valueOf(param.getValue())); break; case PRIMITIVE_SHORT: case SHORT: preparedState.setShort(index, isNull ? null : Short.valueOf(param.getValue())); break; case PRIMITIVE_LONG: case LONG: preparedState.setLong(index, isNull ? null : Long.valueOf(param.getValue())); break; case PRIMITIVE_FLOAT: case FLOAT: preparedState.setFloat(index, isNull ? null : Float.valueOf(param.getValue())); break; case PRIMITIVE_DOUBLE: case DOUBLE: preparedState.setDouble(index, isNull ? null : Double.valueOf(param.getValue())); break; case PRIMITIVE_BOOLEAN: case BOOLEAN: preparedState.setBoolean(index, isNull ? null : Boolean.parseBoolean(param.getValue())); break; case PRIMITIVE_BYTE: case BYTE: preparedState.setByte(index, isNull ? null : Byte.valueOf(param.getValue())); break; case JAVA_UTIL_DATE: case JAVA_SQL_DATE: preparedState.setDate(index, isNull ? null : java.sql.Date.valueOf(param.getValue())); break; case JAVA_SQL_TIME: preparedState.setTime(index, isNull ? null : Time.valueOf(param.getValue())); break; case JAVA_SQL_TIMESTAMP: preparedState.setTimestamp(index, isNull ? null : Timestamp.valueOf(param.getValue())); break; default: preparedState.setObject(index, isNull ? null : param.getValue()); } }
From source file:cz.lbenda.dataman.db.RowDesc.java
@SuppressWarnings("ConstantConditions") private <T> void putToPS(ColumnDesc columnDesc, T value, PreparedStatement ps, int position) throws SQLException { if (value == null) { ps.setObject(position, null);// w w w .j a v a 2 s . c o m return; } BinaryData bd = value instanceof BinaryData ? (BinaryData) value : null; switch (columnDesc.getDataType()) { case STRING: ps.setString(position, (String) value); break; case BOOLEAN: ps.setBoolean(position, (Boolean) value); break; case TIMESTAMP: ps.setTimestamp(position, (Timestamp) value); break; case DATE: ps.setDate(position, (Date) value); break; case TIME: ps.setTime(position, (Time) value); break; case BYTE: ps.setByte(position, (Byte) value); break; case SHORT: ps.setShort(position, (Short) value); break; case INTEGER: ps.setInt(position, (Integer) value); break; case LONG: ps.setLong(position, (Long) value); break; case FLOAT: ps.setFloat(position, (Float) value); break; case DOUBLE: ps.setDouble(position, (Double) value); break; case DECIMAL: ps.setBigDecimal(position, (BigDecimal) value); break; case UUID: ps.setBytes(position, AbstractHelper.uuidToByteArray((UUID) value)); break; case ARRAY: throw new UnsupportedOperationException("The saving changes in ARRAY isn't supported."); // ps.setArray(position, (Array) value); break; // FIXME the value isn't in type java.sql.Array case BYTE_ARRAY: if (bd == null || bd.isNull()) { ps.setBytes(position, null); } else { try { ps.setBytes(position, IOUtils.toByteArray(bd.getInputStream())); } catch (IOException e) { throw new SQLException(e); } } break; case CLOB: if (bd == null || bd.isNull()) { ps.setNull(position, Types.CLOB); } else { ps.setClob(position, bd.getReader()); } break; case BLOB: if (bd == null || bd.isNull()) { ps.setNull(position, Types.BLOB); } else { ps.setBlob(position, bd.getInputStream()); } break; case OBJECT: ps.setObject(position, value); } }
From source file:org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixHBaseAccessor.java
public void saveHostAggregateRecords(Map<TimelineMetric, MetricHostAggregate> hostAggregateMap, String phoenixTableName) throws SQLException { if (hostAggregateMap == null || hostAggregateMap.isEmpty()) { LOG.debug("Empty aggregate records."); return;// w ww. j a v a2 s. c o m } Connection conn = getConnection(); PreparedStatement stmt = null; long start = System.currentTimeMillis(); int rowCount = 0; try { stmt = conn.prepareStatement(String.format(UPSERT_AGGREGATE_RECORD_SQL, phoenixTableName)); for (Map.Entry<TimelineMetric, MetricHostAggregate> metricAggregate : hostAggregateMap.entrySet()) { TimelineMetric metric = metricAggregate.getKey(); MetricHostAggregate hostAggregate = metricAggregate.getValue(); rowCount++; stmt.clearParameters(); stmt.setString(1, metric.getMetricName()); stmt.setString(2, metric.getHostName()); stmt.setString(3, metric.getAppId()); stmt.setString(4, metric.getInstanceId()); stmt.setLong(5, metric.getTimestamp()); stmt.setString(6, metric.getType()); stmt.setDouble(7, hostAggregate.getSum()); stmt.setDouble(8, hostAggregate.getMax()); stmt.setDouble(9, hostAggregate.getMin()); stmt.setDouble(10, hostAggregate.getNumberOfSamples()); try { stmt.executeUpdate(); } catch (SQLException sql) { LOG.error(sql); } if (rowCount >= PHOENIX_MAX_MUTATION_STATE_SIZE - 1) { conn.commit(); rowCount = 0; } } conn.commit(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { // Ignore } } if (conn != null) { try { conn.close(); } catch (SQLException sql) { // Ignore } } } long end = System.currentTimeMillis(); if ((end - start) > 60000l) { LOG.info("Time to save map: " + (end - start) + ", " + "thread = " + Thread.currentThread().getClass()); } }
From source file:sce.Main.java
public String setJobProgress(String fire_instance_id, double progress) { Connection conn = Main.getConnection(); try {//from ww w .j av a2s. c o m PreparedStatement preparedStatement = conn .prepareStatement("UPDATE quartz.QRTZ_STATUS SET PROGRESS = ? WHERE FIRE_INSTANCE_ID = ?"); preparedStatement.setDouble(1, progress); preparedStatement.setString(2, fire_instance_id); preparedStatement.executeUpdate(); } catch (SQLException ex) { Logger.getLogger(ExpressionTree.class.getName()).log(Level.SEVERE, null, ex); return "false"; } finally { try { conn.close(); } catch (SQLException ex) { Logger.getLogger(ExpressionTree.class.getName()).log(Level.SEVERE, null, ex); return ""; } } return "true"; }
From source file:org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixHBaseAccessor.java
/** * Save Metric aggregate records./*from ww w . ja v a 2 s .com*/ * * @throws SQLException */ public void saveClusterAggregateRecords(Map<TimelineClusterMetric, MetricClusterAggregate> records) throws SQLException { if (records == null || records.isEmpty()) { LOG.debug("Empty aggregate records."); return; } long start = System.currentTimeMillis(); Connection conn = getConnection(); PreparedStatement stmt = null; try { stmt = conn.prepareStatement(UPSERT_CLUSTER_AGGREGATE_SQL); int rowCount = 0; for (Map.Entry<TimelineClusterMetric, MetricClusterAggregate> aggregateEntry : records.entrySet()) { TimelineClusterMetric clusterMetric = aggregateEntry.getKey(); MetricClusterAggregate aggregate = aggregateEntry.getValue(); if (LOG.isTraceEnabled()) { LOG.trace("clusterMetric = " + clusterMetric + ", " + "aggregate = " + aggregate); } rowCount++; stmt.clearParameters(); stmt.setString(1, clusterMetric.getMetricName()); stmt.setString(2, clusterMetric.getAppId()); stmt.setString(3, clusterMetric.getInstanceId()); stmt.setLong(4, clusterMetric.getTimestamp()); stmt.setString(5, clusterMetric.getType()); stmt.setDouble(6, aggregate.getSum()); stmt.setInt(7, aggregate.getNumberOfHosts()); stmt.setDouble(8, aggregate.getMax()); stmt.setDouble(9, aggregate.getMin()); try { stmt.executeUpdate(); } catch (SQLException sql) { // we have no way to verify it works!!! LOG.error(sql); } if (rowCount >= PHOENIX_MAX_MUTATION_STATE_SIZE - 1) { conn.commit(); rowCount = 0; } } conn.commit(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { // Ignore } } if (conn != null) { try { conn.close(); } catch (SQLException sql) { // Ignore } } } long end = System.currentTimeMillis(); if ((end - start) > 60000l) { LOG.info("Time to save: " + (end - start) + ", " + "thread = " + Thread.currentThread().getName()); } }
From source file:org.apache.hadoop.yarn.server.applicationhistoryservice.metrics.timeline.PhoenixHBaseAccessor.java
/** * Save Metric aggregate records./*from ww w .ja v a 2 s . com*/ * * @throws SQLException */ public void saveClusterAggregateHourlyRecords(Map<TimelineClusterMetric, MetricHostAggregate> records, String tableName) throws SQLException { if (records == null || records.isEmpty()) { LOG.debug("Empty aggregate records."); return; } long start = System.currentTimeMillis(); Connection conn = getConnection(); PreparedStatement stmt = null; try { stmt = conn.prepareStatement(String.format(UPSERT_CLUSTER_AGGREGATE_TIME_SQL, tableName)); int rowCount = 0; for (Map.Entry<TimelineClusterMetric, MetricHostAggregate> aggregateEntry : records.entrySet()) { TimelineClusterMetric clusterMetric = aggregateEntry.getKey(); MetricHostAggregate aggregate = aggregateEntry.getValue(); if (LOG.isTraceEnabled()) { LOG.trace("clusterMetric = " + clusterMetric + ", " + "aggregate = " + aggregate); } rowCount++; stmt.clearParameters(); stmt.setString(1, clusterMetric.getMetricName()); stmt.setString(2, clusterMetric.getAppId()); stmt.setString(3, clusterMetric.getInstanceId()); stmt.setLong(4, clusterMetric.getTimestamp()); stmt.setString(5, clusterMetric.getType()); stmt.setDouble(6, aggregate.getSum()); // stmt.setInt(7, aggregate.getNumberOfHosts()); stmt.setLong(7, aggregate.getNumberOfSamples()); stmt.setDouble(8, aggregate.getMax()); stmt.setDouble(9, aggregate.getMin()); try { stmt.executeUpdate(); } catch (SQLException sql) { // we have no way to verify it works!!! LOG.error(sql); } if (rowCount >= PHOENIX_MAX_MUTATION_STATE_SIZE - 1) { conn.commit(); rowCount = 0; } } conn.commit(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { // Ignore } } if (conn != null) { try { conn.close(); } catch (SQLException sql) { // Ignore } } } long end = System.currentTimeMillis(); if ((end - start) > 60000l) { LOG.info("Time to save: " + (end - start) + ", " + "thread = " + Thread.currentThread().getName()); } }
From source file:org.nuxeo.ecm.core.storage.sql.jdbc.dialect.DialectPostgreSQL.java
@Override public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column) throws SQLException { switch (column.getJdbcType()) { case Types.VARCHAR: case Types.CLOB: setToPreparedStatementString(ps, index, value, column); return;/*from ww w. j av a 2 s.c o m*/ case Types.BIT: ps.setBoolean(index, ((Boolean) value).booleanValue()); return; case Types.SMALLINT: ps.setInt(index, ((Long) value).intValue()); return; case Types.INTEGER: case Types.BIGINT: ps.setLong(index, ((Number) value).longValue()); return; case Types.DOUBLE: ps.setDouble(index, ((Double) value).doubleValue()); return; case Types.TIMESTAMP: ps.setTimestamp(index, getTimestampFromCalendar((Calendar) value)); return; case Types.ARRAY: int jdbcBaseType = column.getJdbcBaseType(); String jdbcBaseTypeName = column.getSqlBaseTypeString(); if (jdbcBaseType == Types.TIMESTAMP) { value = getTimestampFromCalendar((Serializable[]) value); } Array array = ps.getConnection().createArrayOf(jdbcBaseTypeName, (Object[]) value); ps.setArray(index, array); return; case Types.OTHER: ColumnType type = column.getType(); if (type.isId()) { setId(ps, index, value); return; } else if (type == ColumnType.FTSTORED) { ps.setString(index, (String) value); return; } throw new SQLException("Unhandled type: " + column.getType()); default: throw new SQLException("Unhandled JDBC type: " + column.getJdbcType()); } }