Example usage for java.sql PreparedStatement setBigDecimal

List of usage examples for java.sql PreparedStatement setBigDecimal

Introduction

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

Prototype

void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given java.math.BigDecimal value.

Usage

From source file:net.freechoice.model.orm.Map_Account.java

@Override
public PreparedStatementCreator createUpdate(final FC_Account entity) {

    return new PreparedStatementCreator() {

        @Override/*from  w w  w.  j  a  v  a2  s .  c  om*/
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

            PreparedStatement ps = con.prepareStatement(
                    "update FC_Account set id_user_= ? " + ",balance = ? " + " where id = ? ");

            ps.setInt(1, entity.id_user_);
            ps.setBigDecimal(2, entity.balance);
            ps.setInt(3, entity.id);
            return ps;
        }
    };
}

From source file:net.freechoice.model.orm.Map_Transaction.java

@Override
public PreparedStatementCreator createUpdate(final FC_Transaction entity) {

    return new PreparedStatementCreator() {

        @Override//from w w w.  j av  a  2s. c o  m
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

            PreparedStatement ps = con.prepareStatement("update  FC_Transaction" + " set  id_account_ = ?,"
                    + " amount = ?, " + " comment = ? " + "where id = " + entity.id);
            ps.setInt(1, entity.id_account_);
            ps.setBigDecimal(2, entity.amount);
            ps.setString(3, entity.comment);
            return ps;
        }
    };
}

From source file:net.freechoice.model.orm.Map_Transaction.java

@Override
public PreparedStatementCreator createInsert(final FC_Transaction transaction) {

    return new PreparedStatementCreator() {

        @Override/*from   w ww  . j  a  v a 2 s  .  c o m*/
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement ps = con.prepareStatement(
                    "insert into FC_Transaction(" + "id_account_, " + "amount, comment)" + " values(?, ?, ?)",
                    RET_ID);
            ps.setInt(1, transaction.id_account_);
            ps.setBigDecimal(2, transaction.amount);
            ps.setString(3, transaction.comment);
            return ps;
        }
    };
}

From source file:com.oracle.tutorial.jdbc.StoredProcedureJavaDBSample.java

public static void raisePrice(String coffeeName, double maximumPercentage, BigDecimal[] newPrice)
        throws SQLException {

    Connection con = DriverManager.getConnection("jdbc:default:connection");
    PreparedStatement pstmt = null;
    ResultSet rs = null;// w w w . jav a2s  . com
    BigDecimal oldPrice;

    String queryGetCurrentCoffeePrice = "select COFFEES.PRICE " + "from COFFEES "
            + "where COFFEES.COF_NAME = ?";

    pstmt = con.prepareStatement(queryGetCurrentCoffeePrice);
    pstmt.setString(1, coffeeName);
    rs = pstmt.executeQuery();

    if (rs.next()) {
        oldPrice = rs.getBigDecimal(1);
    } else {
        return;
    }

    BigDecimal maximumNewPrice = oldPrice.multiply(new BigDecimal(1 + maximumPercentage));

    // Test if newPrice[0] > maximumNewPrice

    if (newPrice[0].compareTo(maximumNewPrice) == 1) {
        newPrice[0] = maximumNewPrice;
    }

    // Test if newPrice[0] <= oldPrice

    if (newPrice[0].compareTo(oldPrice) < 1) {
        newPrice[0] = oldPrice;
        return;
    }

    String queryUpdatePrice = "update COFFEES " + "set COFFEES.PRICE = ? " + "where COFFEES.COF_NAME = ?";

    pstmt = con.prepareStatement(queryUpdatePrice);
    pstmt.setBigDecimal(1, newPrice[0]);
    pstmt.setString(2, coffeeName);
    pstmt.executeUpdate();
}

From source file:test.Test_User.java

public void ttx() throws SQLException {
    BigDecimal db = new BigDecimal("15.465482652446425");
    PreparedStatement ps = cpds.getConnection()
            .prepareStatement("insert into fc_transaction(id_account_, amount)" + "values(5, ?)");
    ps.setBigDecimal(1, db);
    ps.execute();//from ww  w  . j  a  va2 s.c  om

    //      ResultSet rs = 
}

From source file:org.gridobservatory.greencomputing.dao.TimeseriesDao.java

protected void insertTimeSeriesAcquisitions(final BigInteger timeSeriesId,
        final List<TimeseriesAcquisitionType> acquisitions) {
    executor.submit(new Runnable() {
        @Override/*from   w w w.  ja  v a 2s  .  c o m*/
        public void run() {
            if (acquisitions != null) {
                getJdbcTemplate().batchUpdate(
                        "insert into time_series_acquisition (time_series_id, ts, value) values (?,?,?)",
                        new BatchPreparedStatementSetter() {

                            @Override
                            public void setValues(PreparedStatement ps, int i) throws SQLException {
                                ps.setLong(1, timeSeriesId.longValue());
                                long dateInMilis = acquisitions.get(i).getTs()
                                        .multiply(BigInteger.valueOf(1000l)).longValue();
                                ps.setTimestamp(2, new Timestamp(dateInMilis));
                                ps.setBigDecimal(3, new BigDecimal(acquisitions.get(i).getV()));
                            }

                            @Override
                            public int getBatchSize() {
                                return acquisitions.size();
                            }
                        });
            }
        }
    });
}

From source file:com.acme.spring.jdbc.repository.impl.JdbcStockRepository.java

/**
 * {@inheritDoc}/*from   ww w  .j a  va  2s .co  m*/
 */
@Override
public long save(final Stock stock) {

    // validates the input
    validateNoNull(stock, "stock");
    validateNotEmpty(stock.getSymbol(), "symbol");

    KeyHolder keyHolder = new GeneratedKeyHolder();
    // performs the insert in the database
    jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into Stock(name, symbol, value, date) values (?, ?, ?, ?)", new String[] { "id" });

            int index = 1;
            ps.setString(index++, stock.getName());
            ps.setString(index++, stock.getSymbol());
            ps.setBigDecimal(index++, stock.getValue());
            ps.setDate(index++, new java.sql.Date(stock.getDate().getTime()));
            return ps;
        }
    }, keyHolder);

    // retrieves the value primary key for the inserted key
    stock.setId((Long) keyHolder.getKey());

    return stock.getId();
}

From source file:mayoapp.migrations.V0400_2024__move_order_items_to_own_table.java

@Override
public void migrate(Connection connection) throws Exception {
    connection.setAutoCommit(false);//from ww w . j a v a2s .  c om

    Statement queryStatement = connection.createStatement();
    ResultSet data = queryStatement.executeQuery("SELECT * from purchase_order");

    List<Order> orders = Lists.newArrayList();
    List<OrderItem> orderItems = Lists.newArrayList();

    ObjectMapper mapper = new ObjectMapper();

    while (data.next()) {
        Order order = new Order();
        order.setId((UUID) data.getObject("entity_id"));

        String orderDataString = data.getString("order_data");
        Map<String, Object> orderData = mapper.readValue(orderDataString,
                new TypeReference<Map<String, Object>>() {
                });

        List<Map<String, Object>> items = (List<Map<String, Object>>) orderData.get("items");

        for (Map<String, Object> item : items) {
            OrderItem orderItem = new OrderItem();

            orderItem.setId(UUID.randomUUID());
            orderItem.setOrderId(order.getId());

            if (item.containsKey("id") && String.class.isAssignableFrom(item.get("id").getClass())) {
                orderItem.setPurchasableId(UUID.fromString((String) item.get("id")));
            }
            orderItem.setType((String) item.get("type"));
            orderItem.setTitle((String) item.get("title"));
            orderItem.setQuantity(((Integer) item.get("quantity")).longValue());
            orderItem.setUnitPrice(BigDecimal.valueOf((Double) item.get("unitPrice")));
            orderItem.setItemTotal(BigDecimal.valueOf((Double) item.get("itemTotal")));
            if (item.containsKey("vatRate")) {
                orderItem.setVatRate(BigDecimal.valueOf((Double) item.get("vatRate")));
            }

            if (item.containsKey("addons")) {
                orderItem.addData("addons", convertAddonsToMap((List<Map<String, Object>>) item.get("addons")));
            }

            orderItems.add(orderItem);
        }

        orderData.remove("items");
        order.setOrderData(orderData);
        orders.add(order);
    }

    queryStatement.close();

    // 1. Update orders

    PreparedStatement updateOrders = connection
            .prepareStatement("UPDATE purchase_order SET order_data = CAST (? AS json) WHERE entity_id =?");

    for (Order order : orders) {
        updateOrders.setObject(1, mapper.writeValueAsString(order.getOrderData()));
        updateOrders.setObject(2, order.getId());
        updateOrders.addBatch();
    }

    updateOrders.executeBatch();

    // 2. Insert items

    PreparedStatement insertItems = connection.prepareStatement(
            "INSERT INTO purchase_order_item (id, order_id, purchasable_id, type, title, quantity, unit_price, "
                    + "item_total, vat_rate, data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CAST (? as json))");

    for (OrderItem item : orderItems) {
        insertItems.setObject(1, item.getId());
        insertItems.setObject(2, item.getOrderId());
        insertItems.setObject(3, item.getPurchasableId());
        insertItems.setString(4, item.getType());
        insertItems.setString(5, item.getTitle());
        insertItems.setLong(6, item.getQuantity());
        insertItems.setBigDecimal(7, item.getUnitPrice());
        insertItems.setBigDecimal(8, item.getItemTotal());
        insertItems.setBigDecimal(9, item.getVatRate());
        insertItems.setString(10, mapper.writeValueAsString(item.getData()));
        insertItems.addBatch();
    }

    insertItems.executeBatch();
}

From source file:gov.nih.nci.cabig.caaers.datamigrator.UserDataMigrator.java

/**
 * This method inserts appropriate records into study personnel table based on existing role_code.
 * @param map//from w w w  . jav  a 2 s .c o  m
 * @param groups
 */
@SuppressWarnings("unchecked")
protected void insertIntoStudyPersonnel(final Map map, final List groups, final boolean onOracleDB) {

    String sql = getInsertStudyPersonnelSql(onOracleDB);
    BatchPreparedStatementSetter setter = null;
    setter = new BatchPreparedStatementSetter() {

        public int getBatchSize() {
            return groups.size();
        }

        public void setValues(PreparedStatement ps, int index) throws SQLException {

            java.sql.Timestamp startDate = (java.sql.Timestamp) map.get("start_date");
            java.sql.Timestamp endDate = (java.sql.Timestamp) map.get("end_date");

            if (onOracleDB) {
                BigDecimal studySiteId = (BigDecimal) map.get("study_sites_id");
                ps.setBigDecimal(1, studySiteId);
            } else {
                int studySiteId = ((Integer) map.get("study_sites_id")).intValue();
                ps.setInt(1, studySiteId);
            }
            ps.setString(2, groups.get(index).toString());
            if (onOracleDB) {
                BigDecimal retiredIndicator = (BigDecimal) map.get("retired_indicator");
                ps.setBigDecimal(3, retiredIndicator);
            } else {
                Boolean retiredIndicator = (Boolean) map.get("retired_indicator");
                ps.setBoolean(3, retiredIndicator);
            }

            ps.setTimestamp(4, startDate);
            ps.setTimestamp(5, endDate);

            if (onOracleDB) {
                BigDecimal siteResearchStaffId = (BigDecimal) map.get("site_research_staffs_id");
                ps.setBigDecimal(6, siteResearchStaffId);
            } else {
                int siteResearchStaffId = ((Integer) map.get("site_research_staffs_id")).intValue();
                ps.setInt(6, siteResearchStaffId);
            }

        }
    };
    getJdbcTemplate().batchUpdate(sql, setter);
}

From source file:gov.nih.nci.cabig.caaers.datamigrator.UserDataMigrator.java

/**
 * This method inserts appropriate records into site_rs_staff_roles table based on existing role_code.
 * @param map/*from   w  w  w . j ava 2  s. c  o  m*/
 * @param groups
 */
@SuppressWarnings("unchecked")
protected void insertIntoSiteResearchStaffRoles(final Map map, final List groups, final boolean onOracleDB) {
    String sql = getInsertSiteResearchStaffRoleSql(onOracleDB);
    BatchPreparedStatementSetter setter = null;
    setter = new BatchPreparedStatementSetter() {

        public int getBatchSize() {
            return groups.size();
        }

        public void setValues(PreparedStatement ps, int index) throws SQLException {

            java.sql.Timestamp startDate = (java.sql.Timestamp) map.get("start_date");
            java.sql.Timestamp endDate = (java.sql.Timestamp) map.get("end_date");

            ps.setString(1, groups.get(index).toString());

            if (onOracleDB) {
                BigDecimal siteResearchStaffId = (BigDecimal) map.get("site_research_staffs_id");
                ps.setBigDecimal(2, siteResearchStaffId);
            } else {
                int siteResearchStaffId = ((Integer) map.get("site_research_staffs_id")).intValue();
                ps.setInt(2, siteResearchStaffId);
            }
            ps.setTimestamp(3, startDate);
            ps.setTimestamp(4, endDate);
        }
    };
    getJdbcTemplate().batchUpdate(sql, setter);
}