List of usage examples for java.sql PreparedStatement setDouble
void setDouble(int parameterIndex, double x) throws SQLException;
double
value. From source file:com.amazonbird.announce.ProductMgrImpl.java
public Product addProduct(Product product) { Connection connection = null; PreparedStatement ps = null; ResultSet rs = null;//from w w w . j a v a 2 s.com try { connection = dbMgr.getConnection(); ps = connection.prepareStatement(ADD_PRODUCT, Statement.RETURN_GENERATED_KEYS); ps.setString(1, product.getName()); ps.setDouble(2, product.getPrice()); ps.setString(3, product.getDestination()); ps.setString(4, product.getAlternativeDestionation()); ps.setString(5, product.getLocale()); ps.setLong(6, product.getAnnouncerId()); ps.executeUpdate(); rs = ps.getGeneratedKeys(); if (rs.next()) { long productId = rs.getLong(1); product.setId(productId); } logger.debug(DBConstants.QUERY_EXECUTION_SUCC + ps.toString()); } catch (MySQLIntegrityConstraintViolationException e) { logger.error("Error: " + e.getMessage() + "\nProduct:" + product.toString()); } catch (SQLException ex) { logger.error(DBConstants.QUERY_EXECUTION_FAIL + ps.toString(), ex); } finally { dbMgr.closeResources(connection, ps, rs); } return product; }
From source file:fll.web.playoff.JsonBracketDataTests.java
private void insertScore(final Connection connection, final int team, final int run, final boolean verified, final double score) throws SQLException { PreparedStatement ps = null; try {//w w w . j av a2 s. c o m ps = connection.prepareStatement( "INSERT INTO Performance (TeamNumber, Tournament, RunNumber, NoShow, Bye, Verified, Score, ComputedTotal)" + " VALUES (?, 2, ?, false, false, ?, ?, ?)"); ps.setInt(1, team); ps.setInt(2, run); ps.setBoolean(3, verified); ps.setDouble(4, score); ps.setDouble(5, score); Assert.assertEquals(1, ps.executeUpdate()); } finally { SQLFunctions.close(ps); } }
From source file:org.xenei.bloomgraph.bloom.sql.MySQLCommands.java
@Override public PreparedStatement pageIndexSearch(final Connection connection, final PageBloomFilter filter) throws SQLException { PreparedStatement stmt = null; try {/*from w ww . j av a 2 s . c o m*/ stmt = connection.prepareStatement( "SELECT bloom, idx FROM PageIndex WHERE hamming>=? AND log>=? AND bloommatch( ?, PageIndex.bloom )"); stmt.setInt(1, filter.getHammingWeight()); stmt.setDouble(2, filter.getApproximateLog(0)); stmt.setBlob(3, DBIO.asInputStream(filter.getByteBuffer())); return stmt; } catch (final SQLException e) { DbUtils.closeQuietly(stmt); throw e; } }
From source file:nl.tudelft.stocktrader.derby.DerbyOrderDAO.java
public int createHolding(Order order) throws DAOException { if (logger.isDebugEnabled()) { logger.debug("OrderDAO.createHolding(OrderDataModel)\nOrderID :" + order.getOrderID() + "\nOrderType :" + order.getOrderType() + "\nSymbol :" + order.getSymbol() + "\nQuantity :" + order.getQuantity() + "\nOrder Status :" + order.getOrderStatus() + "\nOrder Open Date :" + order.getOpenDate() + "\nCompletionDate :" + order.getCompletionDate()); }//from w ww . j a v a 2s . co m PreparedStatement getAccountIdStat = null; int accountId = -1; try { getAccountIdStat = sqlConnection.prepareStatement(SQL_GET_ACCOUNTID_ORDER); getAccountIdStat.setInt(1, order.getOrderID()); ResultSet rs = getAccountIdStat.executeQuery(); if (rs.next()) { accountId = Integer.parseInt(rs.getString(1)); order.setAccountId(accountId); } try { rs.close(); } catch (Exception e) { logger.debug("", e); } } catch (SQLException e) { throw new DAOException( "Exception is thrown when selecting the accountID from order entries where order ID :" + order.getOrderID(), e); } finally { if (getAccountIdStat != null) { try { getAccountIdStat.close(); } catch (Exception e) { logger.debug("", e); } } } if (accountId != -1) { int holdingId = -1; PreparedStatement insertHoldingStat = null; try { insertHoldingStat = sqlConnection.prepareStatement(SQL_INSERT_HOLDING); insertHoldingStat.setBigDecimal(1, order.getPrice()); insertHoldingStat.setDouble(2, order.getQuantity()); Calendar openDate = (order.getOpenDate() != null) ? order.getOpenDate() : Calendar.getInstance(); insertHoldingStat.setDate(3, StockTraderUtility.convertToSqlDate(openDate)); insertHoldingStat.setInt(4, order.getAccountId()); insertHoldingStat.setString(5, order.getSymbol()); insertHoldingStat.executeUpdate(); ResultSet rs = sqlConnection.prepareCall(SQL_GET_LAST_INSERT_ID).executeQuery(); if (rs.next()) { holdingId = rs.getInt(1); } try { rs.close(); } catch (Exception e) { logger.debug("", e); } return holdingId; } catch (SQLException e) { throw new DAOException("An exception is thrown during an insertion of a holding entry", e); } finally { if (insertHoldingStat != null) { try { insertHoldingStat.close(); } catch (Exception e) { logger.debug("", e); } } } } return -1; }
From source file:at.bestsolution.persistence.java.Util.java
public static void setValue(PreparedStatement pstmt, int parameterIndex, TypedValue value) throws SQLException { if (value.value == null) { int sqlType; switch (value.type) { case INT: sqlType = Types.INTEGER; break; case DOUBLE: sqlType = Types.DECIMAL; break; case FLOAT: sqlType = Types.FLOAT; break; case BOOLEAN: sqlType = Types.BOOLEAN; break; case LONG: sqlType = Types.BIGINT; break; case STRING: sqlType = Types.VARCHAR; break; case BLOB: sqlType = Types.BLOB; break; case CLOB: sqlType = Types.CLOB; break; case TIMESTAMP: sqlType = Types.TIMESTAMP; break; default:// www. j a v a 2 s. com sqlType = Types.OTHER; break; } pstmt.setNull(parameterIndex, sqlType); } else { switch (value.type) { case INT: pstmt.setInt(parameterIndex, ((Number) value.value).intValue()); break; case DOUBLE: pstmt.setDouble(parameterIndex, ((Number) value.value).doubleValue()); break; case FLOAT: pstmt.setDouble(parameterIndex, ((Number) value.value).doubleValue()); break; case BOOLEAN: pstmt.setBoolean(parameterIndex, Boolean.TRUE.equals(value.value)); break; case LONG: pstmt.setLong(parameterIndex, ((Number) value.value).longValue()); break; case STRING: pstmt.setString(parameterIndex, (String) value.value); break; case TIMESTAMP: if (value.value instanceof Timestamp) { pstmt.setTimestamp(parameterIndex, (Timestamp) value.value); } else { pstmt.setTimestamp(parameterIndex, new Timestamp(((Date) value.value).getTime())); } break; case UNKNOWN: pstmt.setObject(parameterIndex, value.value); break; default: throw new IllegalStateException("Unknown type"); } } }
From source file:at.alladin.rmbt.statisticServer.OpenTestSearchResource.java
/** * Fills in the given fields in the queue into the given prepared statement * @param ps/* ww w . j a v a2 s. co m*/ * @param searchValues * @param firstField * @return * @throws SQLException */ private static PreparedStatement fillInWhereClause(PreparedStatement ps, Queue<Map.Entry<String, FieldType>> searchValues, int firstField) throws SQLException { //insert all values in the prepared statement in the order //in which the values had been put in the queue for (Map.Entry<String, FieldType> entry : searchValues) { switch (entry.getValue()) { case STRING: ps.setString(firstField, entry.getKey()); break; case DATE: ps.setTimestamp(firstField, new Timestamp(Long.parseLong(entry.getKey()))); break; case LONG: ps.setLong(firstField, Long.parseLong(entry.getKey())); break; case DOUBLE: ps.setDouble(firstField, Double.parseDouble(entry.getKey())); break; case UUID: ps.setObject(firstField, UUID.fromString(entry.getKey())); break; case BOOLEAN: ps.setBoolean(firstField, Boolean.valueOf(entry.getKey())); break; } firstField++; } return ps; }
From source file:org.mskcc.cbio.cgds.dao.DaoProteinArrayData.java
/** * Adds a new ProteinArrayData Record to the Database. * * @param pad ProteinArrayData Object./*w w w. ja va 2 s.c o m*/ * @return number of records successfully added. * @throws DaoException Database Error. */ public int addProteinArrayData(ProteinArrayData pad) throws DaoException { if (getProteinArrayData(pad.getCancerStudyId(), pad.getArrayId(), pad.getCaseId()) != null) { System.err.println("RPPA data of " + pad.getArrayId() + " for case " + pad.getCaseId() + " in cancer study " + pad.getCancerStudyId() + " has already been added."); return 0; } Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = JdbcUtil.getDbConnection(DaoProteinArrayData.class); pstmt = con.prepareStatement( "INSERT INTO protein_array_data (`PROTEIN_ARRAY_ID`,`CANCER_STUDY_ID`,`CASE_ID`,`ABUNDANCE`) " + "VALUES (?,?,?,?)"); pstmt.setString(1, pad.getArrayId()); pstmt.setInt(2, pad.getCancerStudyId()); pstmt.setString(3, pad.getCaseId()); pstmt.setDouble(4, pad.getAbundance()); int rows = pstmt.executeUpdate(); return rows; } catch (SQLException e) { throw new DaoException(e); } finally { JdbcUtil.closeAll(DaoProteinArrayData.class, con, pstmt, rs); } }
From source file:org.mskcc.cbio.portal.dao.DaoProteinArrayData.java
/** * Adds a new ProteinArrayData Record to the Database. * * @param pad ProteinArrayData Object./*from w w w.j a v a 2 s . co m*/ * @return number of records successfully added. * @throws DaoException Database Error. */ public int addProteinArrayData(ProteinArrayData pad) throws DaoException { if (getProteinArrayData(pad.getCancerStudyId(), pad.getArrayId(), pad.getSampleId()) != null) { System.err.println("RPPA data of " + pad.getArrayId() + " for case " + pad.getSampleId() + " in cancer study " + pad.getCancerStudyId() + " has already been added."); return 0; } Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = JdbcUtil.getDbConnection(DaoProteinArrayData.class); pstmt = con.prepareStatement( "INSERT INTO protein_array_data (`PROTEIN_ARRAY_ID`,`CANCER_STUDY_ID`,`SAMPLE_ID`,`ABUNDANCE`) " + "VALUES (?,?,?,?)"); pstmt.setString(1, pad.getArrayId()); pstmt.setInt(2, pad.getCancerStudyId()); pstmt.setInt(3, pad.getSampleId()); pstmt.setDouble(4, pad.getAbundance()); return pstmt.executeUpdate(); } catch (NullPointerException e) { throw new DaoException(e); } catch (SQLException e) { throw new DaoException(e); } finally { JdbcUtil.closeAll(DaoProteinArrayData.class, con, pstmt, rs); } }
From source file:org.openmailarchive.Entities.Mail.java
public boolean insert(Connection conn) throws SQLException { conn.setAutoCommit(false);//ww w .jav a 2 s . c om String insertMail = "INSERT INTO mail(mailid, filepath, mailfrom, dt, subject, body, bodyType, spamScore) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"; String insertRecipient = "INSERT INTO `recipient`(`mailid`, `recipientType`, `recipient`) VALUES(?, ?, ?)"; String insertAttachment = "INSERT INTO `attachment`(`mailid`, `mimeType`, `filename`) VALUES(?, ?, ?)"; try { PreparedStatement stmtMail = conn.prepareStatement(insertMail); stmtMail.setString(1, mailid); stmtMail.setString(2, filepath); stmtMail.setString(3, mailfrom); if (dt == null) dt = Timestamp.from(Instant.now()); stmtMail.setTimestamp(4, dt); stmtMail.setString(5, subject); stmtMail.setString(6, body); stmtMail.setInt(7, bodyType); stmtMail.setDouble(8, spamScore); stmtMail.executeUpdate(); PreparedStatement stmtRecip = conn.prepareStatement(insertRecipient); for (Recipient r : recipients) { stmtRecip.setString(1, mailid); stmtRecip.setString(2, r.getType()); stmtRecip.setString(3, r.getAddress()); stmtRecip.executeUpdate(); } PreparedStatement stmtAttach = conn.prepareStatement(insertAttachment); for (Attachment a : attachments) { stmtAttach.setString(1, mailid); stmtAttach.setString(2, a.getType()); stmtAttach.setString(3, a.getFilename()); stmtAttach.executeUpdate(); } conn.commit(); } catch (SQLException e) { e.printStackTrace(); conn.rollback(); return false; } return true; }
From source file:fitmon.WorkoutData.java
public void addData(String workout, String intensity, int minutes, double calories, String date, int userId) throws IOException, NoSuchAlgorithmException, InvalidKeyException, JSONException, SQLException, ClassNotFoundException {//from w ww . ja v a 2 s. co m //ArrayList arr = new ArrayList(al); PreparedStatement st = null; Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/fitmon", "root", "april-23"); String query = "INSERT into workout (type,calories,date,intensity,duration,userId) values (?,?,?,?,?,?);"; st = conn.prepareStatement(query); conn.setAutoCommit(false); //st.setInt(1,7); st.setString(1, workout); st.setDouble(2, calories); st.setString(3, date); st.setString(4, intensity); st.setInt(5, minutes); st.setInt(6, userId); st.addBatch(); st.executeBatch(); conn.commit(); System.out.println("Record is inserted into workout table!"); st.close(); conn.close(); } catch (SQLException e) { System.out.println(e.getMessage()); conn.rollback(); } finally { if (st != null) { st.close(); } if (conn != null) { conn.close(); } } }