List of usage examples for java.sql PreparedStatement setFloat
void setFloat(int parameterIndex, float x) throws SQLException;
float
value. 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);//from ww w . j a va 2 s . co 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:egovframework.rte.bat.core.item.database.support.EgovMethodMapItemPreparedStatementSetter.java
/** * params ? ? sqlType PreparedStatement? ?? *///from ww w . j a va 2s . c om public void setValues(T item, PreparedStatement ps, String[] params, String[] sqlTypes, Map<String, Method> methodMap) throws SQLException { EgovReflectionSupport<T> reflector = new EgovReflectionSupport<T>(); for (int i = 0; i < params.length; i++) { try { if (sqlTypes[i].equals("String")) { ps.setString(i + 1, (String) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("int")) { ps.setInt(i + 1, (Integer) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("double")) { ps.setDouble(i + 1, (Double) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("Date")) { ps.setDate(i + 1, (Date) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("byte")) { ps.setByte(i + 1, (Byte) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("short")) { ps.setShort(i + 1, (Short) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("boolean")) { ps.setBoolean(i + 1, (Boolean) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("long")) { ps.setLong(i + 1, (Long) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("Float")) { ps.setFloat(i + 1, (Float) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("BigDecimal")) { ps.setBigDecimal(i + 1, (BigDecimal) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("byte[]")) { ps.setBytes(i + 1, (byte[]) reflector.invokeGettterMethod(item, params[i], methodMap)); } else { throw new SQLException(); } } catch (IllegalArgumentException e) { ReflectionUtils.handleReflectionException(e); } } }
From source file:org.ecoinformatics.seek.dataquery.DBTablesGenerator.java
private synchronized PreparedStatement setupPreparedStatmentParameter(int index, PreparedStatement pStatement, String data, String javaDataType) throws SQLException, UnresolvableTypeException, IllegalArgumentException { if (pStatement == null) { return pStatement; }/*from w w w.jav a 2 s . co m*/ // get rid of white space if (data != null) { data = data.trim(); } // set default type as string if (javaDataType == null) { pStatement.setString(index, data); } else { if (javaDataType.equals(STRING)) { pStatement.setString(index, data); } else if (javaDataType.equals(INTEGER)) { pStatement.setInt(index, (new Integer(data)).intValue()); } else if (javaDataType.equals(DOUBLE)) { pStatement.setDouble(index, (new Double(data)).doubleValue()); } else if (javaDataType.equals(FLOAT)) { pStatement.setFloat(index, (new Float(data)).floatValue()); } else if (javaDataType.equals(BOOLEAN)) { pStatement.setBoolean(index, (new Boolean(data)).booleanValue()); } else if (javaDataType.equals(LONG)) { pStatement.setLong(index, (new Long(data)).longValue()); } else if (javaDataType.equals(DATETIME)) { pStatement.setTimestamp(index, Timestamp.valueOf(data)); } else { throw new UnresolvableTypeException("This java type " + javaDataType + " has NOT implement in " + "DBTablesGenerator.setupPreparedStatmentParameter method"); } } return pStatement; }
From source file:nl.nn.adapterframework.jdbc.XmlQuerySender.java
private void applyParameters(PreparedStatement statement, Vector columns) throws SQLException { Iterator iter = columns.iterator(); int var = 1; while (iter.hasNext()) { Column column = (Column) iter.next(); if (column.getParameter() != null) { if (column.getParameter() instanceof String) { log.debug(// w w w . ja v a 2 s .c o m "parm [" + var + "] is a String with value [" + column.getParameter().toString() + "]"); statement.setString(var, (String) column.getParameter()); var++; } if (column.getParameter() instanceof Integer) { log.debug("parm [" + var + "] is an Integer with value [" + column.getParameter().toString() + "]"); statement.setInt(var, Integer.parseInt(column.getParameter().toString())); var++; } if (column.getParameter() instanceof Float) { log.debug( "parm [" + var + "] is a Float with value [" + column.getParameter().toString() + "]"); statement.setFloat(var, Float.parseFloat(column.getParameter().toString())); var++; } if (column.getParameter() instanceof Timestamp) { log.debug("parm [" + var + "] is a Timestamp with value [" + column.getParameter().toString() + "]"); statement.setTimestamp(var, (Timestamp) column.getParameter()); var++; } } } }
From source file:org.ojbc.adapters.analyticaldatastore.dao.AnalyticalDatastoreDAOImpl.java
@Override public Integer saveDisposition(final Disposition inboundDisposition) { log.debug("Inserting row into Disposition table"); KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { String dispositionInsertStatement = ""; String[] insertArgs = null; //No disposition ID provided in POJO if (inboundDisposition.getDispositionID() == null) { dispositionInsertStatement = "INSERT into Disposition (PersonID,DispositionTypeID,IncidentCaseNumber,DispositionDate,ArrestingAgencyORI," + "SentenceTermDays,SentenceFineAmount,InitialChargeCode, FinalChargeCode, RecordType,IsProbationViolation,IsProbationViolationOnOldCharge,RecidivismEligibilityDate, DocketChargeNumber,InitialChargeCode1, FinalChargeCode1) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; insertArgs = new String[] { "PersonID", "DispositionTypeID", "IncidentCaseNumber", "DispositionDate,ArrestingAgencyORI," + "SentenceTermDays", "SentenceFineAmount", "InitialChargeCode", "FinalChargeCode", "RecordType", "IsProbationViolation", "IsProbationViolationOnOldCharge", "RecidivismEligibilityDate", "DocketChargeNumber", "InitialChargeCode1", "FinalChargeCode1" }; }//from w w w .ja v a2 s . com //Disposition ID provided in POJO else { dispositionInsertStatement = "INSERT into Disposition (PersonID,DispositionTypeID,IncidentCaseNumber,DispositionDate,ArrestingAgencyORI," + "SentenceTermDays,SentenceFineAmount,InitialChargeCode, FinalChargeCode, RecordType,IsProbationViolation,IsProbationViolationOnOldCharge,RecidivismEligibilityDate, DocketChargeNumber, InitialChargeCode1, FinalChargeCode1,DispositionID) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; insertArgs = new String[] { "PersonID", "DispositionTypeID", "IncidentCaseNumber", "DispositionDate,ArrestingAgencyORI," + "SentenceTermDays", "SentenceFineAmount", "InitialChargeCode", "FinalChargeCode", "RecordType", "IsProbationViolation", "IsProbationViolationOnOldCharge", "RecidivismEligibilityDate", "DocketChargeNumber", "InitialChargeCode1", "FinalChargeCode1", "DispositionID" }; } PreparedStatement ps = connection.prepareStatement(dispositionInsertStatement, insertArgs); ps.setInt(1, inboundDisposition.getPersonID()); ps.setInt(2, inboundDisposition.getDispositionTypeID()); ps.setString(3, inboundDisposition.getIncidentCaseNumber()); ps.setDate(4, new java.sql.Date(inboundDisposition.getDispositionDate().getTime())); ps.setString(5, inboundDisposition.getArrestingAgencyORI()); if (inboundDisposition.getSentenceTermDays() != null) { ps.setBigDecimal(6, inboundDisposition.getSentenceTermDays()); } else { ps.setNull(6, java.sql.Types.NULL); } if (inboundDisposition.getSentenceFineAmount() != null) { ps.setFloat(7, inboundDisposition.getSentenceFineAmount()); } else { ps.setNull(7, java.sql.Types.NULL); } ps.setString(8, inboundDisposition.getInitialChargeCode()); ps.setString(9, inboundDisposition.getFinalChargeCode()); ps.setString(10, String.valueOf(inboundDisposition.getRecordType())); if (inboundDisposition.getIsProbationViolation() != null) { ps.setString(11, String.valueOf(inboundDisposition.getIsProbationViolation())); } else { ps.setNull(11, java.sql.Types.NULL); } if (inboundDisposition.getIsProbationViolationOnOldCharge() != null) { ps.setString(12, String.valueOf(inboundDisposition.getIsProbationViolationOnOldCharge())); } else { ps.setNull(12, java.sql.Types.NULL); } if (inboundDisposition.getRecidivismEligibilityDate() != null) { ps.setDate(13, new java.sql.Date(inboundDisposition.getRecidivismEligibilityDate().getTime())); } else { ps.setNull(13, java.sql.Types.NULL); } ps.setString(14, inboundDisposition.getDocketChargeNumber()); ps.setString(15, inboundDisposition.getInitialChargeRank()); ps.setString(16, inboundDisposition.getFinalChargeRank()); if (inboundDisposition.getDispositionID() != null) { ps.setInt(17, inboundDisposition.getDispositionID()); } return ps; } }, keyHolder); Integer returnValue = null; if (inboundDisposition.getDispositionID() != null) { returnValue = inboundDisposition.getDispositionID(); } else { returnValue = keyHolder.getKey().intValue(); } return returnValue; }
From source file:org.dspace.storage.rdbms.DatabaseManager.java
/** * Iterate over the given parameters and add them to the given prepared statement. * Only a select number of datatypes are supported by the JDBC driver. * * @param statement/*from w w w.j a va 2 s. c om*/ * The unparameterized statement. * @param parameters * The parameters to be set on the statement. */ protected static void loadParameters(PreparedStatement statement, Object[] parameters) throws SQLException { statement.clearParameters(); int idx = 1; for (Object parameter : parameters) { if (parameter instanceof String) { statement.setString(idx, (String) parameter); } else if (parameter instanceof Long) { statement.setLong(idx, ((Long) parameter).longValue()); } else if (parameter instanceof Integer) { statement.setInt(idx, ((Integer) parameter).intValue()); } else if (parameter instanceof Short) { statement.setShort(idx, ((Short) parameter).shortValue()); } else if (parameter instanceof Date) { statement.setDate(idx, (Date) parameter); } else if (parameter instanceof Time) { statement.setTime(idx, (Time) parameter); } else if (parameter instanceof Timestamp) { statement.setTimestamp(idx, (Timestamp) parameter); } else if (parameter instanceof Double) { statement.setDouble(idx, ((Double) parameter).doubleValue()); } else if (parameter instanceof Float) { statement.setFloat(idx, ((Float) parameter).floatValue()); } else if (parameter == null) { throw new SQLException("Attempting to insert null value into SQL query."); } else { throw new SQLException("Attempting to insert unknown datatype (" + parameter.getClass().getName() + ") into SQL statement."); } idx++; } }
From source file:mysql5.MySQL5PlayerDAO.java
/** * {@inheritDoc}//from ww w .j av a 2s. co m */ @Override public void storePlayer(final Player player) { Connection con = null; try { con = DatabaseFactory.getConnection(); PreparedStatement stmt = con.prepareStatement( "UPDATE players SET name=?, exp=?, recoverexp=?, x=?, y=?, z=?, heading=?, world_id=?, gender=?, race=?, player_class=?, last_online=?, quest_expands=?, npc_expands=?, advanced_stigma_slot_size=?, warehouse_size=?, note=?, title_id=?, bonus_title_id=?, dp=?, soul_sickness=?, mailbox_letters=?, reposte_energy=?, event_exp=?, bg_points=?, mentor_flag_time=?, initial_gamestats=?, world_owner=?, fatigue=?, fatigueRecover=?, fatigueReset=?, stamps=?, rewarded_pass=?, last_stamp=?, joinRequestLegionId=?, joinRequestState=? WHERE id=?"); log.debug("[DAO: MySQL5PlayerDAO] storing player " + player.getObjectId() + " " + player.getName()); PlayerCommonData pcd = player.getCommonData(); stmt.setString(1, player.getName()); stmt.setLong(2, pcd.getExp()); stmt.setLong(3, pcd.getExpRecoverable()); stmt.setFloat(4, player.getX()); stmt.setFloat(5, player.getY()); stmt.setFloat(6, player.getZ()); stmt.setInt(7, player.getHeading()); stmt.setInt(8, player.getWorldId()); stmt.setString(9, player.getGender().toString()); stmt.setString(10, player.getRace().toString()); stmt.setString(11, pcd.getPlayerClass().toString()); stmt.setTimestamp(12, pcd.getLastOnline()); stmt.setInt(13, player.getQuestExpands()); stmt.setInt(14, player.getNpcExpands()); stmt.setInt(15, pcd.getAdvancedStigmaSlotSize()); stmt.setInt(16, player.getWarehouseSize()); stmt.setString(17, pcd.getNote()); stmt.setInt(18, pcd.getTitleId()); stmt.setInt(19, pcd.getBonusTitleId()); stmt.setInt(20, pcd.getDp()); stmt.setInt(21, pcd.getDeathCount()); Mailbox mailBox = player.getMailbox(); int mails = mailBox != null ? mailBox.size() : pcd.getMailboxLetters(); stmt.setInt(22, mails); stmt.setLong(23, pcd.getCurrentReposteEnergy()); stmt.setLong(24, pcd.getCurrentEventExp()); stmt.setInt(25, player.getCommonData().getBattleGroundPoints()); stmt.setInt(26, pcd.getMentorFlagTime()); stmt.setInt(27, pcd.isInitialGameStats()); if (player.getPosition().getWorldMapInstance() == null) { log.error("Error saving player: " + player.getObjectId() + " " + player.getName() + ", world map instance is null. Setting world owner to 0. Position: " + player.getWorldId() + " " + player.getX() + " " + player.getY() + " " + player.getZ()); stmt.setInt(28, 0); } else { stmt.setInt(28, player.getPosition().getWorldMapInstance().getOwnerId()); } stmt.setInt(29, pcd.getFatigue()); stmt.setInt(30, pcd.getFatigueRecover()); stmt.setInt(31, pcd.getFatigueReset()); stmt.setInt(32, pcd.getPassportStamps()); stmt.setInt(33, pcd.getPassportReward()); stmt.setTimestamp(34, pcd.getLastStamp()); stmt.setInt(35, pcd.getJoinRequestLegionId()); stmt.setString(36, pcd.getJoinRequestState().name()); stmt.setInt(37, player.getObjectId()); stmt.execute(); stmt.close(); } catch (Exception e) { log.error("Error saving player: " + player.getObjectId() + " " + player.getName(), e); } finally { DatabaseFactory.close(con); } if (CacheConfig.CACHE_COMMONDATA) { PlayerCommonData cached = playerCommonData.get(player.getObjectId()); if (cached != null) { playerCommonData.putEntry(player.getCommonData().getPlayerObjId(), player.getCommonData()); playerCommonDataByName.putEntry(player.getName().toLowerCase(), player.getCommonData()); } } }
From source file:org.apache.jmeter.protocol.jdbc.AbstractJDBCTestElement.java
private void setArgument(PreparedStatement pstmt, String argument, int targetSqlType, int index) throws SQLException { switch (targetSqlType) { case Types.INTEGER: pstmt.setInt(index, Integer.parseInt(argument)); break;//from ww w .j a va2 s .c o m case Types.DECIMAL: case Types.NUMERIC: pstmt.setBigDecimal(index, new BigDecimal(argument)); break; case Types.DOUBLE: case Types.FLOAT: pstmt.setDouble(index, Double.parseDouble(argument)); break; case Types.CHAR: case Types.LONGVARCHAR: case Types.VARCHAR: pstmt.setString(index, argument); break; case Types.BIT: case Types.BOOLEAN: pstmt.setBoolean(index, Boolean.parseBoolean(argument)); break; case Types.BIGINT: pstmt.setLong(index, Long.parseLong(argument)); break; case Types.DATE: pstmt.setDate(index, Date.valueOf(argument)); break; case Types.REAL: pstmt.setFloat(index, Float.parseFloat(argument)); break; case Types.TINYINT: pstmt.setByte(index, Byte.parseByte(argument)); break; case Types.SMALLINT: pstmt.setShort(index, Short.parseShort(argument)); break; case Types.TIMESTAMP: pstmt.setTimestamp(index, Timestamp.valueOf(argument)); break; case Types.TIME: pstmt.setTime(index, Time.valueOf(argument)); break; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: pstmt.setBytes(index, argument.getBytes()); break; case Types.NULL: pstmt.setNull(index, targetSqlType); break; default: pstmt.setObject(index, argument, targetSqlType); } }
From source file:org.waarp.common.database.data.AbstractDbData.java
/** * Set Value into PreparedStatement/*ww w . jav a 2s . c om*/ * * @param ps * @param value * @param rank * >= 1 * @throws WaarpDatabaseSqlException */ static public void setTrueValue(PreparedStatement ps, DbValue value, int rank) throws WaarpDatabaseSqlException { try { switch (value.type) { case Types.VARCHAR: if (value.value == null) { ps.setNull(rank, Types.VARCHAR); break; } ps.setString(rank, (String) value.value); break; case Types.LONGVARCHAR: if (value.value == null) { ps.setNull(rank, Types.LONGVARCHAR); break; } ps.setString(rank, (String) value.value); break; case Types.BIT: if (value.value == null) { ps.setNull(rank, Types.BIT); break; } ps.setBoolean(rank, (Boolean) value.value); break; case Types.TINYINT: if (value.value == null) { ps.setNull(rank, Types.TINYINT); break; } ps.setByte(rank, (Byte) value.value); break; case Types.SMALLINT: if (value.value == null) { ps.setNull(rank, Types.SMALLINT); break; } ps.setShort(rank, (Short) value.value); break; case Types.INTEGER: if (value.value == null) { ps.setNull(rank, Types.INTEGER); break; } ps.setInt(rank, (Integer) value.value); break; case Types.BIGINT: if (value.value == null) { ps.setNull(rank, Types.BIGINT); break; } ps.setLong(rank, (Long) value.value); break; case Types.REAL: if (value.value == null) { ps.setNull(rank, Types.REAL); break; } ps.setFloat(rank, (Float) value.value); break; case Types.DOUBLE: if (value.value == null) { ps.setNull(rank, Types.DOUBLE); break; } ps.setDouble(rank, (Double) value.value); break; case Types.VARBINARY: if (value.value == null) { ps.setNull(rank, Types.VARBINARY); break; } ps.setBytes(rank, (byte[]) value.value); break; case Types.DATE: if (value.value == null) { ps.setNull(rank, Types.DATE); break; } ps.setDate(rank, (Date) value.value); break; case Types.TIMESTAMP: if (value.value == null) { ps.setNull(rank, Types.TIMESTAMP); break; } ps.setTimestamp(rank, (Timestamp) value.value); break; case Types.CLOB: if (value.value == null) { ps.setNull(rank, Types.CLOB); break; } ps.setClob(rank, (Reader) value.value); break; case Types.BLOB: if (value.value == null) { ps.setNull(rank, Types.BLOB); break; } ps.setBlob(rank, (InputStream) value.value); break; default: throw new WaarpDatabaseSqlException("Type not supported: " + value.type + " at " + rank); } } catch (ClassCastException e) { throw new WaarpDatabaseSqlException("Setting values casting error: " + value.type + " at " + rank, e); } catch (SQLException e) { DbSession.error(e); throw new WaarpDatabaseSqlException("Setting values in error: " + value.type + " at " + rank, e); } }
From source file:org.apache.gora.sql.store.SqlStore.java
/** * Sets the object to the preparedStatement by it's schema *//* w w w . java 2 s.com*/ public void setObject(PreparedStatement statement, int index, Object object, Schema schema, Column column) throws SQLException, IOException { Type type = schema.getType(); switch (type) { case MAP: setField(statement, column, schema, index, object); break; case ARRAY: setField(statement, column, schema, index, object); break; case BOOLEAN: statement.setBoolean(index, (Boolean) object); break; case BYTES: setBytes(statement, column, index, ((ByteBuffer) object).array()); break; case DOUBLE: statement.setDouble(index, (Double) object); break; case ENUM: statement.setString(index, ((Enum<?>) object).name()); break; case FIXED: setBytes(statement, column, index, ((GenericFixed) object).bytes()); break; case FLOAT: statement.setFloat(index, (Float) object); break; case INT: statement.setInt(index, (Integer) object); break; case LONG: statement.setLong(index, (Long) object); break; case NULL: break; case RECORD: setField(statement, column, schema, index, object); break; case STRING: statement.setString(index, ((Utf8) object).toString()); break; case UNION: throw new IOException("Union is not supported yet"); } }