Example usage for java.sql PreparedStatement setDouble

List of usage examples for java.sql PreparedStatement setDouble

Introduction

In this page you can find the example usage for java.sql PreparedStatement setDouble.

Prototype

void setDouble(int parameterIndex, double x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java double value.

Usage

From source file:org.carlspring.tools.csv.dao.CSVDao.java

private void setField(PreparedStatement ps, int i, Field field, String value) throws SQLException {
    // Handle primitives
    if (field.getType().equals("int")) {
        ps.setInt(i, StringUtils.isBlank(value) ? 0 : Integer.parseInt(value));
    } else if (field.getType().equals("long")) {
        ps.setLong(i, StringUtils.isBlank(value) ? 0l : Long.parseLong(value));
    } else if (field.getType().equals("float")) {
        ps.setFloat(i, StringUtils.isBlank(value) ? 0f : Float.parseFloat(value));
    } else if (field.getType().equals("double")) {
        ps.setDouble(i, StringUtils.isBlank(value) ? 0d : Double.parseDouble(value));
    } else if (field.getType().equals("boolean")) {
        ps.setBoolean(i, StringUtils.isBlank(value) && Boolean.parseBoolean(value));
    }/*w w w  . j  a va2s. c  om*/
    // Handle objects
    else if (field.getType().equals("java.lang.String")) {
        ps.setString(i, StringUtils.isBlank(value) ? null : value);
    } else if (field.getType().equals("java.sql.Date")) {
        ps.setDate(i, StringUtils.isBlank(value) ? null : Date.valueOf(value));
    } else if (field.getType().equals("java.sql.Timestamp")) {
        ps.setTimestamp(i, StringUtils.isBlank(value) ? null : Timestamp.valueOf(value));
    } else if (field.getType().equals("java.math.BigDecimal")) {
        ps.setBigDecimal(i, StringUtils.isBlank(value) ? null : new BigDecimal(Long.parseLong(value)));
    }
}

From source file:com.hexin.core.dao.BaseDaoSupport.java

private void setPreparedStatementParameter(PreparedStatement ps, Object[] args) throws SQLException {

    if (args == null || args.length <= 0) {
        return;/*  www  . ja v a2  s  .co m*/
    }

    for (int i = 0; i < args.length; i++) {
        String clzName = args[i].getClass().getName();
        int psIndex = i + 1;

        if (clzName.equals("java.lang.Integer")) {
            ps.setInt(psIndex, (Integer) args[i]);
        } else if (clzName.equals("java.lang.Double")) {
            ps.setDouble(psIndex, (Double) args[i]);
        } else if (clzName.equals("java.lang.Float")) {
            ps.setDouble(psIndex, (Float) args[i]);
        } else if (clzName.equals("java.lang.Long")) {
            ps.setLong(psIndex, (Long) args[i]);
        } else {
            ps.setString(psIndex, (String) args[i]);
        }
    }

}

From source file:dept_integration.Dept_Integchalan.java

public void doInsert() throws Exception {
    User u = new User();
    u.createConnection();/*from w  w  w  . java 2 s.  c  om*/

    Connection con = u.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "insert into department_epayment(CHALLAN_NO,AMOUNT,BANK_NAME,TREASURY_CODE,NAME,DEPT_CODE,TRANS_DATE,TRANSID,BANK_REFNO,CIN,STATUS,MISC) values(?,?,?,?,?,?,?,?,?,?,?,?)";
    try {
        ps = con.prepareStatement("select * from department_epayment where challan_No=?");
        ps.setString(1, this.CHALLAN_NO);
        rs = ps.executeQuery();
        if (rs.next()) {
            throw new Exception("Challan_no Already Exists,Please generate a new one.");
        } else {
            ps = con.prepareStatement(sql);
            ps.setString(1, this.CHALLAN_NO);
            ps.setDouble(2, this.AMOUNT);
            ps.setString(3, this.BANK_NAME);
            ps.setString(4, this.TREASURY_CODE);
            ps.setString(5, this.NAME);
            ps.setString(6, this.DEPT_CODE);
            ps.setString(7, this.TRANS_DATE);
            ps.setString(8, this.TRANSID);
            ps.setString(9, this.BANK_REFNO);
            ps.setString(10, this.CIN);
            ps.setString(11, this.STATUS);
            ps.setString(12, this.BANK_NAME);
            ps.execute();
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw e;
    } finally {
        try {
            if (ps != null)
                ps.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw e;
        }
    }
}

From source file:org.mskcc.cbio.cgds.dao.DaoClinicalData.java

/**
 * Add a New Case.//from  ww w . ja  va 2s  . com
 *
 * @param caseId                    Case ID.
 * @param overallSurvivalMonths     Overall Survival Months.
 * @param overallSurvivalStatus     Overall Survival Status.
 * @param diseaseFreeSurvivalMonths Disease Free Survival Months.
 * @param diseaseFreeSurvivalStatus Disease Free Survival Status.
 * @return number of cases added.
 * @throws DaoException Error Adding new Record.
 */
public int addCase(int cancerStudyId, String caseId, Double overallSurvivalMonths, String overallSurvivalStatus,
        Double diseaseFreeSurvivalMonths, String diseaseFreeSurvivalStatus, Double ageAtDiagnosis)
        throws DaoException {
    if (caseId == null || caseId.trim().length() == 0) {
        throw new IllegalArgumentException("Case ID is null or empty");
    }
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = JdbcUtil.getDbConnection(DaoClinicalData.class);
        pstmt = con.prepareStatement(
                "INSERT INTO clinical (`CANCER_STUDY_ID`, `CASE_ID`, `OVERALL_SURVIVAL_MONTHS`, "
                        + "`OVERALL_SURVIVAL_STATUS`, "
                        + "`DISEASE_FREE_SURVIVAL_MONTHS`, `DISEASE_FREE_SURVIVAL_STATUS`,"
                        + "`AGE_AT_DIAGNOSIS`) " + "VALUES (?,?,?,?,?,?,?)");
        pstmt.setInt(1, cancerStudyId);
        pstmt.setString(2, caseId);

        //  Make sure to set to Null if we are missing data.
        if (overallSurvivalMonths == null) {
            pstmt.setNull(3, java.sql.Types.DOUBLE);
        } else {
            pstmt.setDouble(3, overallSurvivalMonths);
        }

        if (overallSurvivalStatus == null) {
            pstmt.setNull(4, java.sql.Types.VARCHAR);
        } else {
            pstmt.setString(4, overallSurvivalStatus);
        }

        if (diseaseFreeSurvivalMonths == null) {
            pstmt.setNull(5, java.sql.Types.DOUBLE);
        } else {
            pstmt.setDouble(5, diseaseFreeSurvivalMonths);
        }

        if (diseaseFreeSurvivalStatus == null) {
            pstmt.setNull(6, java.sql.Types.VARCHAR);
        } else {
            pstmt.setString(6, diseaseFreeSurvivalStatus);
        }

        if (ageAtDiagnosis == null) {
            pstmt.setNull(7, java.sql.Types.DOUBLE);
        } else {
            pstmt.setDouble(7, ageAtDiagnosis);
        }

        int rows = pstmt.executeUpdate();
        return rows;
    } catch (SQLException e) {
        throw new DaoException(e);
    } finally {
        JdbcUtil.closeAll(DaoClinicalData.class, con, pstmt, rs);
    }
}

From source file:com.concursive.connect.web.modules.productcatalog.dao.Product.java

/**
 * Description of the Method/* ww  w  .  j a  va  2  s  . c om*/
 *
 * @param db Description of the Parameter
 * @return Description of the Return Value
 * @throws SQLException Description of the Exception
 */
public boolean insert(Connection db) throws SQLException {
    PreparedStatement pst = db.prepareStatement("INSERT INTO customer_order_product "
            + "(order_id, product_id, product_name, price_description, total_price) VALUES (?,?,?,?,?) ");
    int i = 0;
    pst.setInt(++i, orderId);
    pst.setInt(++i, id);
    pst.setString(++i, name);
    pst.setString(++i, priceDescription);
    pst.setDouble(++i, this.getTotalPrice());
    pst.execute();
    pst.close();
    orderItemId = DatabaseUtils.getCurrVal(db, "customer_order_product_item_id_seq", -1);
    // Insert the selected options
    optionList.setOrderItemId(orderItemId);
    optionList.insert(db);
    return true;
}

From source file:com.dbmojo.QueryExecutor.java

/** Add all the values in the String[] to the pstmt PreparedStatment
  * Use some regex action to figure out what data type each value is
  * before setting it/*www.  java 2 s . c o  m*/
  */
private void setPreparedStatementValues(PreparedStatement pstmt, String[] values) throws Exception {
    final int vLen = values.length;
    for (int v = 0; v < vLen; v++) {
        final String val = values[v];
        final int idx = v + 1;
        final Matcher intMatcher = intPattern.matcher(val);
        final Matcher doubleMatcher = doublePattern.matcher(val);

        if (intMatcher.find()) {
            pstmt.setInt(idx, Integer.parseInt(val));
        } else if (doubleMatcher.find()) {
            pstmt.setDouble(idx, Double.parseDouble(val));
        } else {
            pstmt.setString(idx, val);
        }
    }
}

From source file:com.act.lcms.db.model.StandardWell.java

protected void bindInsertOrUpdateParameters(PreparedStatement stmt, Integer plateId, Integer plateRow,
        Integer plateColumn, String chemical, String media, String note, Double concentration)
        throws SQLException {
    stmt.setInt(DB_FIELD.PLATE_ID.getInsertUpdateOffset(), plateId);
    stmt.setInt(DB_FIELD.PLATE_ROW.getInsertUpdateOffset(), plateRow);
    stmt.setInt(DB_FIELD.PLATE_COLUMN.getInsertUpdateOffset(), plateColumn);
    stmt.setString(DB_FIELD.CHEMICAL.getInsertUpdateOffset(), chemical);
    stmt.setString(DB_FIELD.MEDIA.getInsertUpdateOffset(), media);
    stmt.setString(DB_FIELD.NOTE.getInsertUpdateOffset(), note);
    if (concentration != null) {
        stmt.setDouble(DB_FIELD.CONCENTRATION.getInsertUpdateOffset(), concentration);
    } else {/*from  ww w  . j  ava 2 s.  c om*/
        stmt.setNull(DB_FIELD.CONCENTRATION.getInsertUpdateOffset(), Types.DOUBLE);
    }
}

From source file:org.apache.phoenix.query.BaseTest.java

protected static void initSumDoubleValues(byte[][] splits, String url) throws Exception {
    ensureTableCreated(url, "SumDoubleTest", splits);
    Properties props = new Properties();
    Connection conn = DriverManager.getConnection(url, props);
    try {/*from  w  w  w .  j  ava2 s  . com*/
        // Insert all rows at ts
        PreparedStatement stmt = conn.prepareStatement("upsert into " + "SumDoubleTest(" + "    id, "
                + "    d, " + "    f, " + "    ud, " + "    uf) " + "VALUES (?, ?, ?, ?, ?)");
        stmt.setString(1, "1");
        stmt.setDouble(2, 0.001);
        stmt.setFloat(3, 0.01f);
        stmt.setDouble(4, 0.001);
        stmt.setFloat(5, 0.01f);
        stmt.execute();

        stmt.setString(1, "2");
        stmt.setDouble(2, 0.002);
        stmt.setFloat(3, 0.02f);
        stmt.setDouble(4, 0.002);
        stmt.setFloat(5, 0.02f);
        stmt.execute();

        stmt.setString(1, "3");
        stmt.setDouble(2, 0.003);
        stmt.setFloat(3, 0.03f);
        stmt.setDouble(4, 0.003);
        stmt.setFloat(5, 0.03f);
        stmt.execute();

        stmt.setString(1, "4");
        stmt.setDouble(2, 0.004);
        stmt.setFloat(3, 0.04f);
        stmt.setDouble(4, 0.004);
        stmt.setFloat(5, 0.04f);
        stmt.execute();

        stmt.setString(1, "5");
        stmt.setDouble(2, 0.005);
        stmt.setFloat(3, 0.05f);
        stmt.setDouble(4, 0.005);
        stmt.setFloat(5, 0.05f);
        stmt.execute();

        conn.commit();
    } finally {
        conn.close();
    }
}

From source file:com.act.lcms.db.model.FeedingLCMSWell.java

protected void bindInsertOrUpdateParameters(PreparedStatement stmt, Integer plateId, Integer plateRow,
        Integer plateColumn, String msid, String composition, String extract, String chemical,
        Double concentration, String note) throws SQLException {
    stmt.setInt(DB_FIELD.PLATE_ID.getInsertUpdateOffset(), plateId);
    stmt.setInt(DB_FIELD.PLATE_ROW.getInsertUpdateOffset(), plateRow);
    stmt.setInt(DB_FIELD.PLATE_COLUMN.getInsertUpdateOffset(), plateColumn);
    stmt.setString(DB_FIELD.MSID.getInsertUpdateOffset(), msid);
    stmt.setString(DB_FIELD.COMPOSITION.getInsertUpdateOffset(), composition);
    stmt.setString(DB_FIELD.EXTRACT.getInsertUpdateOffset(), extract);
    stmt.setString(DB_FIELD.CHEMICAL.getInsertUpdateOffset(), chemical);
    if (concentration != null) {
        stmt.setDouble(DB_FIELD.CONCENTRATION.getInsertUpdateOffset(), concentration);
    } else {/*from  w w  w .j  a v  a  2  s.c  om*/
        stmt.setNull(DB_FIELD.CONCENTRATION.getInsertUpdateOffset(), Types.DOUBLE);
    }
    stmt.setString(DB_FIELD.NOTE.getInsertUpdateOffset(), note);
}

From source file:com.liferay.portal.upgrade.util.Table.java

public void setColumn(PreparedStatement ps, int index, Integer type, String value) throws Exception {

    int t = type.intValue();

    int paramIndex = index + 1;

    if (t == Types.BIGINT) {
        ps.setLong(paramIndex, GetterUtil.getLong(value));
    } else if (t == Types.BOOLEAN) {
        ps.setBoolean(paramIndex, GetterUtil.getBoolean(value));
    } else if ((t == Types.CLOB) || (t == Types.VARCHAR)) {
        value = StringUtil.replace(value, SAFE_CHARS[1], SAFE_CHARS[0]);

        ps.setString(paramIndex, value);
    } else if (t == Types.DOUBLE) {
        ps.setDouble(paramIndex, GetterUtil.getDouble(value));
    } else if (t == Types.FLOAT) {
        ps.setFloat(paramIndex, GetterUtil.getFloat(value));
    } else if (t == Types.INTEGER) {
        ps.setInt(paramIndex, GetterUtil.getInteger(value));
    } else if (t == Types.SMALLINT) {
        ps.setShort(paramIndex, GetterUtil.getShort(value));
    } else if (t == Types.TIMESTAMP) {
        if (StringPool.NULL.equals(value)) {
            ps.setTimestamp(paramIndex, null);
        } else {/*ww  w  . j  ava  2 s  .  c om*/
            DateFormat df = DateUtil.getISOFormat();

            ps.setTimestamp(paramIndex, new Timestamp(df.parse(value).getTime()));
        }
    } else {
        throw new UpgradeException("Upgrade code using unsupported class type " + type);
    }
}