List of usage examples for java.sql PreparedStatement setDouble
void setDouble(int parameterIndex, double x) throws SQLException;
double
value. From source file:org.nuxeo.ecm.core.storage.sql.db.dialect.DialectOracle.java
@Override public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column) throws SQLException { switch (column.getJdbcType()) { case Types.VARCHAR: case Types.CLOB: String v;/*from w w w. ja v a2s . com*/ if (column.getType() == ColumnType.BLOBID) { v = ((Binary) value).getDigest(); } else { v = (String) value; } ps.setString(index, v); break; case Types.BIT: ps.setBoolean(index, ((Boolean) value).booleanValue()); return; case Types.TINYINT: case Types.SMALLINT: ps.setInt(index, ((Long) value).intValue()); return; case Types.INTEGER: case Types.BIGINT: ps.setLong(index, ((Long) value).longValue()); return; case Types.DOUBLE: ps.setDouble(index, ((Double) value).doubleValue()); return; case Types.TIMESTAMP: Calendar cal = (Calendar) value; Timestamp ts = new Timestamp(cal.getTimeInMillis()); ps.setTimestamp(index, ts, cal); // cal passed for timezone return; // case Types.OTHER: // if (column.getType() == Type.FTSTORED) { // ps.setString(index, (String) value); // return; // } // throw new SQLException("Unhandled type: " + column.getType()); default: throw new SQLException("Unhandled JDBC type: " + column.getJdbcType()); } }
From source file:connectivity.connection.java
public void sortPH() throws SQLException { PreparedStatement ps = con.prepareStatement("Select tip_id,ph from tips where neg is NULL; "); ResultSet rs = ps.executeQuery(); ArrayList likes = new ArrayList(); while (rs.next()) { String[] arr = new String[2]; arr[0] = rs.getString("tip_id"); arr[1] = rs.getString("ph"); likes.add(arr);//from ww w . j ava2 s . c om } //System.out.println(likes); try { for (Object like : likes) { String[] arr = (String[]) like; JSONObject json = (JSONObject) new JSONParser().parse(arr[1]); //System.out.print(json.get("groups").toString()); JSONObject probability = (JSONObject) new JSONParser().parse((json.get("probability").toString())); String label = (String) json.get("label"); double neg = (double) probability.get("neg"); double neutral = (double) probability.get("neutral"); double pos = (double) probability.get("pos"); ps = con.prepareStatement( "UPDATE `4sreviews`.`tips` SET `neg`=?, `neutral`=?, `pos`=?, `label`=? WHERE `tip_id`=?;"); ps.setDouble(1, neg); ps.setDouble(2, neutral); ps.setDouble(3, pos); ps.setString(4, label); ps.setString(5, arr[0]); ps.execute(); System.out.print('a'); } } catch (Exception e) { System.out.println("exception e=" + e); } }
From source file:org.dspace.storage.rdbms.MockDatabaseManager.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// w ww .j av a 2 s .co m * The unparameterized statement. * @param parameters * The parameters to be set on the statement. */ @Mock protected static void loadParameters(PreparedStatement statement, Object[] parameters) throws SQLException { statement.clearParameters(); for (int i = 0; i < parameters.length; i++) { // Select the object we are setting. Object parameter = parameters[i]; int idx = i + 1; // JDBC starts counting at 1. if (parameter == null) { throw new SQLException("Attempting to insert null value into SQL query."); } if (parameter instanceof String) { statement.setString(idx, (String) parameters[i]); } else if (parameter instanceof Integer) { int ii = ((Integer) parameter).intValue(); statement.setInt(idx, ii); } else if (parameter instanceof Double) { double d = ((Double) parameter).doubleValue(); statement.setDouble(idx, d); } else if (parameter instanceof Float) { float f = ((Float) parameter).floatValue(); statement.setFloat(idx, f); } else if (parameter instanceof Short) { short s = ((Short) parameter).shortValue(); statement.setShort(idx, s); } else if (parameter instanceof Long) { long l = ((Long) parameter).longValue(); statement.setLong(idx, l); } else if (parameter instanceof Date) { Date date = (Date) parameter; statement.setDate(idx, date); } else if (parameter instanceof Time) { Time time = (Time) parameter; statement.setTime(idx, time); } else if (parameter instanceof Timestamp) { Timestamp timestamp = (Timestamp) parameter; statement.setTimestamp(idx, timestamp); } else { throw new SQLException("Attempting to insert unknown datatype (" + parameter.getClass().getName() + ") into SQL statement."); } } }
From source file:com.ibm.bluemix.samples.PostgreSQLClient.java
/** * Insert text into PostgreSQL/* ww w .j a v a2 s .c om*/ * * param posts List of Strings of text to insert * * @return number of rows affected * @throws Exception * @throws Exception */ public int addProfile(String notesID, String password, String name, String pemID, String ilID, String techDomain, String techOther, double utilization, String location, String onSiteFlag, String onBenchFlag, String regiesteredFlag) throws Exception { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("INSERT INTO profile ( "); sqlBuilder.append("NotesID "); sqlBuilder.append(",Password "); sqlBuilder.append(",Name "); sqlBuilder.append(",PeMID "); sqlBuilder.append(",ILID "); sqlBuilder.append(",TechDomain "); sqlBuilder.append(",TechOther "); sqlBuilder.append(",Utilization "); sqlBuilder.append(",Location "); sqlBuilder.append(",OnSiteFlag "); sqlBuilder.append(",OnBenchFlag "); sqlBuilder.append(",RegiesteredFlag "); sqlBuilder.append(",RoleID "); sqlBuilder.append(") VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,? )"); Connection connection = null; PreparedStatement statement = null; try { connection = getConnection(); statement = connection.prepareStatement(sqlBuilder.toString()); statement.setString(1, notesID); statement.setString(2, password); statement.setString(3, name); statement.setString(4, pemID); statement.setString(5, ilID); statement.setString(6, techDomain); statement.setString(7, techOther); statement.setDouble(8, utilization); statement.setString(9, location); statement.setString(10, onSiteFlag); statement.setString(11, onBenchFlag); statement.setString(12, regiesteredFlag); System.out.println(notesID); if (notesID.equals("notes@cn.ibm.com")) { System.out.println("true"); statement.setString(13, "1000"); } else { System.out.println("False"); statement.setString(13, "1001"); } 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:org.jobjects.dao.annotation.ManagerTools.java
protected final void setAll(PreparedStatement pstmt, int i, Object obj) throws SQLException { if (obj instanceof Boolean) { pstmt.setBoolean(i, ((Boolean) obj).booleanValue()); }/* w ww .ja v a 2 s .c o m*/ if (obj instanceof Byte) { pstmt.setByte(i, ((Byte) obj).byteValue()); } if (obj instanceof Short) { pstmt.setShort(i, ((Short) obj).shortValue()); } if (obj instanceof Integer) { pstmt.setInt(i, ((Integer) obj).intValue()); } if (obj instanceof Long) { pstmt.setLong(i, ((Long) obj).longValue()); } if (obj instanceof Float) { pstmt.setFloat(i, ((Float) obj).floatValue()); } if (obj instanceof Double) { pstmt.setDouble(i, ((Double) obj).doubleValue()); } if (obj instanceof Timestamp) { pstmt.setTimestamp(i, ((Timestamp) obj)); } if (obj instanceof Date) { pstmt.setDate(i, ((Date) obj)); } if (obj instanceof BigDecimal) { pstmt.setBigDecimal(i, ((BigDecimal) obj)); } if (obj instanceof String) { pstmt.setString(i, ((String) obj)); } }
From source file:org.apache.gora.sql.store.SqlStore.java
/** * Sets the object to the preparedStatement by it's schema *///from w w w . j av a 2 s . co m 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"); } }
From source file:com.exploringspatial.dao.impl.ConflictDaoImpl.java
@Override public void batchUpdate(List<Conflict> conflicts) { jdbcTemplate.batchUpdate(batchUpdateSql, new BatchPreparedStatementSetter() { @Override// w w w.j av a 2 s .com 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(); } }); }
From source file:org.wso2.carbon.social.sql.SQLActivityPublisher.java
/** * rating cache consists of pre-populated rating-average values for a * particular target. We update rating cache when there is a new rating * activity.Rating activity always occurs with a comment activity. * /*from ww w. jav a 2 s . co m*/ * @param connection * @param targetId * @param rating * @throws SQLException */ private void updateRatingCache(Connection connection, String targetId, int rating) throws SQLException { ResultSet resultSet = null; PreparedStatement selectCacheStatement; PreparedStatement updateCacheStatement; PreparedStatement insertCacheStatement; try { if (log.isDebugEnabled()) { log.debug("Executing: " + SELECT_CACHE_SQL); } selectCacheStatement = connection.prepareStatement(SELECT_CACHE_SQL); selectCacheStatement.setString(1, targetId); resultSet = selectCacheStatement.executeQuery(); if (!resultSet.next()) { //TODO get rid of this block as we are warming up the cache during asset creation String tenantDomain = SocialUtil.getTenantDomain(); if (log.isDebugEnabled()) { log.debug("Executing: " + INSERT_CACHE_SQL); } insertCacheStatement = connection.prepareStatement(INSERT_CACHE_SQL); insertCacheStatement.setString(1, targetId); insertCacheStatement.setInt(2, rating); insertCacheStatement.setInt(3, 1); insertCacheStatement.setDouble(4, rating); insertCacheStatement.setString(5, tenantDomain); insertCacheStatement.executeUpdate(); } else { int total, count; total = resultSet.getInt(Constants.RATING_TOTAL) + rating; count = resultSet.getInt(Constants.RATING_COUNT) + 1; if (log.isDebugEnabled()) { log.debug("Executing: " + UPDATE_CACHE_SQL); } updateCacheStatement = connection.prepareStatement(UPDATE_CACHE_SQL); updateCacheStatement.setInt(1, total); updateCacheStatement.setInt(2, count); updateCacheStatement.setDouble(3, (double) total / count); updateCacheStatement.setString(4, targetId); updateCacheStatement.executeUpdate(); } } catch (SQLException e) { log.error("Unable to update the cache. " + e.getMessage(), e); throw e; } }
From source file:org.wso2.carbon.appmgt.migration.client.MigrationClientImpl.java
private void migrateMobileAppRatings(Map<String, Float> appRating, String tenantDomain) throws APPMMigrationException { Connection connection = null; PreparedStatement statement = null; try {/* w w w.j a va2 s .c o m*/ if (log.isDebugEnabled()) { log.debug("Executing: " + Constants.INSERT_SOCIAL_CACHE); } connection = getConnection(Constants.SOCIAL_DB_NAME); statement = connection.prepareStatement(Constants.INSERT_SOCIAL_CACHE); for (String contextId : appRating.keySet()) { statement.setString(1, contextId); Float rating = appRating.get(contextId); statement.setInt(2, rating.intValue()); statement.setInt(3, 1); statement.setDouble(4, rating.doubleValue()); statement.setString(5, tenantDomain); statement.addBatch(); } statement.executeBatch(); } catch (SQLException e) { handleException("Error occurred while migrating mobile application ratings for tenant " + tenantDomain, e); } catch (DataSourceException e) { handleException("Error occurred while obtaining datasource connection for " + Constants.SOCIAL_DB_NAME + " during mobile application ratings migration for tenant " + tenantDomain, e); } finally { closeConnection(connection); if (statement != null) { try { statement.close(); } catch (SQLException e) { handleException( "Error occurred while closing prepared statement for Mobile app Social Cache update " + "for tenant " + tenantDomain, 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 ww. j a va 2 s. com // 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; }