List of usage examples for java.sql PreparedStatement setDouble
void setDouble(int parameterIndex, double x) throws SQLException;
double
value. From source file:org.easyrec.utils.spring.store.dao.DaoUtils.java
public static void setDouble(PreparedStatement stmt, Double value, int index) throws SQLException { if (value == null) { stmt.setNull(index, Types.DOUBLE); return;//w w w . ja v a 2s . c om } stmt.setDouble(index, value); }
From source file:com.act.lcms.db.model.CuratedChemical.java
protected static void bindInsertOrUpdateParameters(PreparedStatement stmt, String name, String inchi, Double mPlusHPlusMass, Integer expectedCollisionVoltage, String referenceUrl) throws SQLException { stmt.setString(DB_FIELD.NAME.getInsertUpdateOffset(), name); stmt.setString(DB_FIELD.INCHI.getInsertUpdateOffset(), inchi); stmt.setDouble(DB_FIELD.MASS.getInsertUpdateOffset(), mPlusHPlusMass); if (expectedCollisionVoltage != null) { stmt.setInt(DB_FIELD.EXPECTED_COLLISION_VOLTAGE.getInsertUpdateOffset(), expectedCollisionVoltage); } else {/*from ww w .jav a2s. c om*/ stmt.setNull(DB_FIELD.EXPECTED_COLLISION_VOLTAGE.getInsertUpdateOffset(), Types.INTEGER); } stmt.setString(DB_FIELD.REFERENCE_URL.getInsertUpdateOffset(), referenceUrl); }
From source file:org.wso2.extension.siddhi.store.rdbms.util.RDBMSTableUtils.java
/** * Util method which is used to populate a {@link PreparedStatement} instance with a single element. * * @param stmt the statement to which the element should be set. * @param ordinal the ordinal of the element in the statement (its place in a potential list of places). * @param type the type of the element to be set, adheres to * {@link org.wso2.siddhi.query.api.definition.Attribute.Type}. * @param value the value of the element. * @throws SQLException if there are issues when the element is being set. *//*from w w w . ja v a2 s.c o m*/ public static void populateStatementWithSingleElement(PreparedStatement stmt, int ordinal, Attribute.Type type, Object value) throws SQLException { switch (type) { case BOOL: stmt.setBoolean(ordinal, (Boolean) value); break; case DOUBLE: stmt.setDouble(ordinal, (Double) value); break; case FLOAT: stmt.setFloat(ordinal, (Float) value); break; case INT: stmt.setInt(ordinal, (Integer) value); break; case LONG: stmt.setLong(ordinal, (Long) value); break; case OBJECT: stmt.setObject(ordinal, value); break; case STRING: stmt.setString(ordinal, (String) value); break; } }
From source file:yelpproject.DatabaseConnection.java
public static void insertIntoBusinessTable(String business_id, String categories, long review_count, String name, double stars) throws SQLException { PreparedStatement preparedStatement = null; String insertTableSQL = "INSERT INTO BUSINESS" + " VALUES(?,?,?,?,?)"; try {// ww w. ja v a2 s.c o m if (connection == null) { connection = getDBConnection(); } preparedStatement = connection.prepareStatement(insertTableSQL); preparedStatement.setString(1, business_id); preparedStatement.setString(2, categories); preparedStatement.setLong(3, review_count); preparedStatement.setString(4, name); preparedStatement.setDouble(5, stars); // execute insert SQL stetement preparedStatement.executeUpdate(); System.out.println("Record is inserted into BUSINESS table!"); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { if (preparedStatement != null) { preparedStatement.close(); } } }
From source file:simrankapp.util.ReaderWriterUtil.java
public static void writeSimRankInDB(SparseRealMatrix simRankMatrix) { Connection con = null;/* www .ja v a 2 s . c o m*/ PreparedStatement stmt = null; try { Class.forName(TheCrawlersConstants.JDBC_DRIVER); System.out.println("Connecting to a selected database..."); con = DriverManager.getConnection(TheCrawlersConstants.DB_URL, TheCrawlersConstants.DB_USERNAME, TheCrawlersConstants.DB_PASSWORD); System.out.println("Connected database successfully..."); stmt = con.prepareStatement(TheCrawlersConstants.INSERT_SIMRANK_SQL); int numNodes = simRankMatrix.getRowDimension(); for (int row = 0; row < numNodes; row++) { for (int col = 0; col < numNodes; col++) { double value = simRankMatrix.getEntry(row, col); if (value > 0) { stmt.setInt(1, row); stmt.setInt(2, col); stmt.setDouble(3, value); stmt.addBatch(); } } } stmt.executeBatch(); } catch (Exception e) { e.printStackTrace(); } finally { try { stmt.close(); con.close(); } catch (Exception e) { } } }
From source file:org.castor.jdo.engine.SQLTypeInfos.java
/** * Set given value on given PreparedStatement at given index with given SQL type. * /*from ww w . j a va2 s.c o m*/ * @param stmt The PreparedStatement to set value on. * @param index The index of the value in the PreparedStatement. * @param value The value to set. * @param sqlType The SQL type of the value. */ public static void setValue(final PreparedStatement stmt, final int index, final Object value, final int sqlType) { try { if (value == null) { stmt.setNull(index, sqlType); } else { // Special processing for BLOB and CLOB types, because they are mapped // by Castor to java.io.InputStream and java.io.Reader, respectively, // while JDBC driver expects java.sql.Blob and java.sql.Clob. switch (sqlType) { case Types.FLOAT: case Types.DOUBLE: stmt.setDouble(index, ((Double) value).doubleValue()); break; case Types.REAL: stmt.setFloat(index, ((Float) value).floatValue()); break; case Types.TIME: final Time time = value instanceof java.util.Date ? new Time(((java.util.Date) value).getTime()) : null; stmt.setTime(index, time != null ? time : (Time) value, getCalendar()); break; case Types.DATE: final Date date = value instanceof java.util.Date ? new Date(((java.util.Date) value).getTime()) : null; stmt.setDate(index, date != null ? date : (Date) value); break; case Types.TIMESTAMP: final Timestamp timestamp = value instanceof java.util.Date ? new Timestamp(((java.util.Date) value).getTime()) : null; stmt.setTimestamp(index, timestamp != null ? timestamp : (Timestamp) value, getCalendar()); break; case Types.BLOB: try { InputStream stream; if (value instanceof byte[]) { stream = new ByteArrayInputStream((byte[]) value); } else { stream = (InputStream) value; } stmt.setBinaryStream(index, stream, stream.available()); } catch (IOException ex) { throw new SQLException(ex.toString()); } break; case Types.CLOB: if (value instanceof String) { stmt.setCharacterStream(index, new StringReader((String) value), Math.min(((String) value).length(), Integer.MAX_VALUE)); } else { stmt.setCharacterStream(index, ((Clob) value).getCharacterStream(), (int) Math.min(((Clob) value).length(), Integer.MAX_VALUE)); } break; default: stmt.setObject(index, value, sqlType); break; } } } catch (SQLException ex) { LOG.error("Unexpected SQL exception: ", ex); } }
From source file:yelpproject.DatabaseConnection.java
public static void insertIntoYelpUserTable(java.sql.Date yelping_since, JSONObject votes, long review_count, String name, String user_id, int friends, long fans, double average_stars, String type, JSONObject compliments, JSONArray elite) throws SQLException { PreparedStatement preparedStatement = null; String insertTableSQL = "INSERT INTO YELP_USER" + " VALUES(?,?,?,?,?,?,?,?,?,?,?)"; try {//from w w w . ja va2 s . c o m if (connection == null) { connection = getDBConnection(); } preparedStatement = connection.prepareStatement(insertTableSQL); preparedStatement.setDate(1, yelping_since); preparedStatement.setString(2, String.valueOf(votes)); preparedStatement.setString(3, name); preparedStatement.setLong(4, review_count); preparedStatement.setString(5, user_id); preparedStatement.setInt(6, friends); preparedStatement.setLong(7, fans); preparedStatement.setDouble(8, average_stars); preparedStatement.setString(9, type); preparedStatement.setString(10, String.valueOf(compliments)); preparedStatement.setString(11, String.valueOf(elite)); // execute insert SQL stetement preparedStatement.executeUpdate(); System.out.println("Record is inserted into YELP_USER table!"); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { if (preparedStatement != null) { preparedStatement.close(); } } }
From source file:org.biokoframework.system.repository.sql.translator.annotation.impl.DoubleTranslator.java
@Override public void insertIntoStatement(String fieldName, Object fieldValue, Field fieldAnnotation, PreparedStatement statement, int sqlIndex) throws SQLException { statement.setDouble(sqlIndex, (Double) fieldValue); }
From source file:org.rhq.plugins.database.DatabasePluginUtil.java
private static void bindParameters(PreparedStatement statement, Object... parameters) throws SQLException { int i = 1;/*w ww.ja v a 2s . c o m*/ for (Object p : parameters) { if (p instanceof String) { statement.setString(i++, (String) p); } else if (p instanceof Byte) { statement.setByte(i++, (Byte) p); } else if (p instanceof Short) { statement.setShort(i++, (Short) p); } else if (p instanceof Integer) { statement.setInt(i++, (Integer) p); } else if (p instanceof Long) { statement.setLong(i++, (Long) p); } else if (p instanceof Float) { statement.setFloat(i++, (Float) p); } else if (p instanceof Double) { statement.setDouble(i++, (Double) p); } else { statement.setObject(i++, p); } } }
From source file:org.apache.phoenix.util.TestUtil.java
/** * @param conn/*from ww w .ja va2s. c o m*/ * connection to be used * @param sortOrder * sort order of column contain input values * @param id * id of the row being inserted * @param input * input to be inserted */ public static void upsertRow(Connection conn, String tableName, String sortOrder, int id, Object input) throws SQLException { String dml = String.format("UPSERT INTO " + tableName + "_%s VALUES(?,?)", sortOrder); PreparedStatement stmt = conn.prepareStatement(dml); stmt.setInt(1, id); if (input instanceof String) stmt.setString(2, (String) input); else if (input instanceof Integer) stmt.setInt(2, (Integer) input); else if (input instanceof Double) stmt.setDouble(2, (Double) input); else if (input instanceof Float) stmt.setFloat(2, (Float) input); else if (input instanceof Boolean) stmt.setBoolean(2, (Boolean) input); else if (input instanceof Long) stmt.setLong(2, (Long) input); else throw new UnsupportedOperationException("" + input.getClass() + " is not supported by upsertRow"); stmt.execute(); conn.commit(); }