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:nl.tudelft.stocktrader.derby.DerbyCustomerDAO.java

public void updateAccountBalance(int accountId, BigDecimal total) throws DAOException {
    if (logger.isDebugEnabled()) {
        logger.debug("MySQLCustomerDAO.updateAccoutBalance(int,BigDecimal)\n Account ID :" + accountId
                + "\nTotal :" + total);
    }/* w w w.  j  a  va2 s .co  m*/
    PreparedStatement debitAccountStat = null;
    try {
        /* Tiago: we need to be very careful with BigDecimals. Round to 2 places */
        total = total.setScale(2, RoundingMode.HALF_UP);

        debitAccountStat = sqlConnection.prepareStatement(SQL_DEBIT_ACCOUNT);
        debitAccountStat.setBigDecimal(1, total);
        debitAccountStat.setInt(2, accountId);
        debitAccountStat.executeUpdate();

    } catch (SQLException e) {
        throw new DAOException("Excpetion is thrown when updating the account balance for accountID :"
                + accountId + " total :" + total, e);
    } finally {
        if (debitAccountStat != null) {
            try {
                debitAccountStat.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
}

From source file:nl.tudelft.stocktrader.derby.DerbyCustomerDAO.java

public boolean insertAccount(Account accountBean) throws DAOException {
    PreparedStatement insertAccount = null;
    boolean insertSuccess = false;
    try {/*  w ww.j  a  v a  2s .  c om*/
        insertAccount = sqlConnection.prepareStatement(SQL_INSERT_ACCOUNT);
        insertAccount.setBigDecimal(1, accountBean.getOpenBalance());
        insertAccount.setInt(2, 0);
        insertAccount.setBigDecimal(3, accountBean.getOpenBalance());
        //first insert: the user didn't exist before, no last login
        //            insertAccount.setDate(4, StockTraderUtility.convertToSqlDate(accountBean.getLastLogin()));
        insertAccount.setInt(4, 0);
        insertAccount.setString(5, accountBean.getUserID());
        insertAccount.setString(6, accountBean.getCurrencyType());
        insertAccount.executeUpdate();
        insertSuccess = true;

    } catch (SQLException e) {
        insertSuccess = false;
        throw new DAOException("", e);

    } finally {
        if (insertAccount != null) {
            try {
                insertAccount.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
    return insertSuccess;
}

From source file:nl.tudelft.stocktrader.derby.DerbyCustomerDAO.java

public boolean insertWallet(Wallet wallet) throws DAOException {
    PreparedStatement insertWallet = null;
    boolean insertSuccess = false;
    try {/*from  www  .  j  a  va2  s. c o m*/
        insertWallet = sqlConnection.prepareStatement(SQL_INSERT_WALLET);
        insertWallet.setString(1, wallet.getUserID());
        insertWallet.setBigDecimal(2, wallet.getUsd());
        insertWallet.setBigDecimal(3, wallet.getEur());
        insertWallet.setBigDecimal(4, wallet.getGbp());
        insertWallet.setBigDecimal(5, wallet.getCny());
        insertWallet.setBigDecimal(6, wallet.getInr());
        insertWallet.executeUpdate();
        insertSuccess = true;
    } catch (SQLException e) {
        insertSuccess = false;
        throw new DAOException("", e);
    } finally {
        if (insertWallet != null) {
            try {
                insertWallet.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
    return insertSuccess;
}

From source file:nl.tudelft.stocktrader.derby.DerbyCustomerDAO.java

public Wallet updateWallet(Wallet wallet) throws DAOException {
    PreparedStatement updateWallet = null;
    try {//w w  w.j  a v  a  2 s.c  om
        updateWallet = sqlConnection.prepareStatement(SQL_UPDATE_WALLET);
        updateWallet.setBigDecimal(1, wallet.getUsd());
        updateWallet.setBigDecimal(2, wallet.getEur());
        updateWallet.setBigDecimal(3, wallet.getGbp());
        updateWallet.setBigDecimal(4, wallet.getCny());
        updateWallet.setBigDecimal(5, wallet.getInr());
        updateWallet.setString(6, wallet.getUserID());
        updateWallet.executeUpdate();
        return wallet;
    } catch (SQLException e) {
        throw new DAOException("", e);
    } finally {
        if (updateWallet != null) {
            try {
                updateWallet.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
}

From source file:nl.tudelft.stocktrader.derby.DerbyMarketSummaryDAO.java

public void updateStockPriceVolume(double quantity, RemoteQuoteData quote) throws DAOException {
    //      BigDecimal priceChangeFactor = StockTraderUtility.getRandomPriceChangeFactor(quote.getPrice());
    //      BigDecimal newPrice = quote.getPrice().multiply(priceChangeFactor);
    ///*  w ww . ja v  a2 s.co  m*/
    //      if (newPrice.compareTo(quote.getLow()) == -1) {
    //         quote.setLow(newPrice);
    //      }
    //      if (newPrice.compareTo(quote.getHigh()) == 1) {
    //         quote.setHigh(newPrice);
    //      }

    PreparedStatement previousValues = null;
    PreparedStatement updateStockPriceVolumeStat = null;
    try {
        previousValues = sqlConnection.prepareStatement(SQL_SELECT_QUOTE);
        previousValues.setString(1, quote.getTicker());
        ResultSet rs = previousValues.executeQuery();

        if (!rs.next()) {
            throw new DAOException("Could not find quote " + quote.getTicker());
        }

        double low = rs.getDouble("low");
        double high = rs.getDouble("high");

        if (low > quote.getValue()) {
            low = quote.getValue();
        }

        if (high < quote.getValue()) {
            high = quote.getValue();
        }

        BigDecimal value = new BigDecimal(quote.getValue()).setScale(4, RoundingMode.HALF_UP);

        updateStockPriceVolumeStat = sqlConnection.prepareStatement(SQL_UPDATE_STOCKPRICEVOLUME);
        updateStockPriceVolumeStat.setBigDecimal(1, value);
        updateStockPriceVolumeStat.setBigDecimal(2, new BigDecimal(low).setScale(4, RoundingMode.HALF_UP));
        updateStockPriceVolumeStat.setBigDecimal(3, new BigDecimal(high).setScale(4, RoundingMode.HALF_UP));
        updateStockPriceVolumeStat.setBigDecimal(4, value);
        updateStockPriceVolumeStat.setFloat(5, (float) quantity);
        updateStockPriceVolumeStat.setString(6, quote.getTicker());
        updateStockPriceVolumeStat.executeUpdate();

    } catch (SQLException e) {
        throw new DAOException("", e);
    } finally {
        try {
            if (updateStockPriceVolumeStat != null) {
                updateStockPriceVolumeStat.close();
            }
        } catch (SQLException e) {
            logger.debug("", 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   www.jav a2  s.  c  o  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:nl.tudelft.stocktrader.derby.DerbyOrderDAO.java

public void closeOrder(Order order) throws DAOException {
    if (logger.isDebugEnabled()) {
        logger.debug("OrderDAO.closeOrder(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 ww  w  .j av  a2 s .  c  o  m

    PreparedStatement closeOrderStat = null;
    try {
        closeOrderStat = sqlConnection.prepareStatement(SQL_CLOSE_ORDER);
        closeOrderStat.setString(1, StockTraderUtility.ORDER_STATUS_CLOSED);
        if (StockTraderUtility.ORDER_TYPE_SELL.equals(order.getOrderType())) {
            closeOrderStat.setNull(2, Types.INTEGER);
        } else {
            closeOrderStat.setInt(2, order.getHoldingId());
        }
        closeOrderStat.setBigDecimal(3, order.getPrice());
        closeOrderStat.setInt(4, order.getOrderID());
        closeOrderStat.executeUpdate();

    } catch (SQLException e) {
        throw new DAOException("", e);

    } finally {
        if (closeOrderStat != null) {
            try {
                closeOrderStat.close();
            } catch (Exception e) {
                logger.debug("", e);
            }
        }
    }
}

From source file:nl.tudelft.stocktrader.derby.DerbyOrderDAO.java

public Order createOrder(String userID, String symbol, String orderType, double quantity, int holdingID)
        throws DAOException {
    int orderID = 0;
    Calendar minCalender = Calendar.getInstance();
    minCalender.setTimeInMillis(0);//from  w  ww.  j a  v a2 s. c o  m
    Order order = new Order(orderID, orderType, StockTraderUtility.ORDER_STATUS_OPEN, Calendar.getInstance(),
            minCalender, quantity, BigDecimal.valueOf(1), StockTraderUtility.getOrderFee(orderType), symbol);
    order.setHoldingId(holdingID);

    PreparedStatement getAccountId = null;
    try {
        getAccountId = sqlConnection.prepareStatement(SQL_GET_ACCOUNTID);
        getAccountId.setString(1, userID);
        ResultSet rs = getAccountId.executeQuery();
        if (rs.next()) {
            order.setAccountId(rs.getInt(1));
        }
    } catch (SQLException e) {
        throw new DAOException("", e);
    } finally {
        if (getAccountId != null) {
            try {
                getAccountId.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }

    PreparedStatement insertOrder = null;
    PreparedStatement selectOrderID = null;
    try {
        insertOrder = sqlConnection.prepareStatement(SQL_INSERT_ORDER);
        insertOrder.setBigDecimal(1, order.getOrderFee());
        insertOrder.setBigDecimal(2, order.getPrice());
        insertOrder.setString(3, order.getSymbol());
        // FIXED: metro used Double rather than double
        //         insertOrder.setFloat(4, (float) order.getQuantity());
        insertOrder.setFloat(4, order.getQuantity().floatValue());
        insertOrder.setString(5, order.getOrderType());
        insertOrder.setInt(6, order.getAccountId());
        insertOrder.setInt(7, order.getHoldingId());
        insertOrder.executeUpdate();

        selectOrderID = sqlConnection.prepareStatement(SQL_SELECT_ORDER_ID);
        // ORDERFEE = ? AND PRICE = ? AND QUOTE_SYMBOL = ? AND QUANTITY = ?
        // ORDERTYPE = ? ORDERSTATUS = ? AND ACCOUNT_ACCOUNTID = ?
        // HOLDING_HOLDINGID = ?"
        selectOrderID.setBigDecimal(1, order.getOrderFee());
        selectOrderID.setBigDecimal(2, order.getPrice());
        selectOrderID.setString(3, order.getSymbol());
        selectOrderID.setDouble(4, order.getQuantity());
        selectOrderID.setString(5, order.getOrderType());
        selectOrderID.setString(6, "open");
        selectOrderID.setInt(7, order.getAccountId());
        selectOrderID.setInt(8, order.getHoldingId());
        ResultSet rs = selectOrderID.executeQuery();
        if (rs.next()) {
            try {
                order.setOrderID(rs.getInt(1));
            } finally {
                try {
                    rs.close();
                } catch (SQLException e) {
                    logger.debug("", e);
                }
            }
        }
    } catch (SQLException e) {
        throw new DAOException("", e);
    } finally {
        if (insertOrder != null) {
            try {
                insertOrder.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
        if (selectOrderID != null) {
            try {
                selectOrderID.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
    return order;
}

From source file:nl.tudelft.stocktrader.mysql.MySQLCustomerDAO.java

public void updateAccountBalance(int accountId, BigDecimal total) throws DAOException {
    if (logger.isDebugEnabled()) {
        logger.debug("MySQLCustomerDAO.updateAccoutBalance(int,BigDecimal)\n Account ID :" + accountId
                + "\nTotal :" + total);
    }/*www. ja  v a  2  s.  c om*/
    PreparedStatement debitAccountStat = null;
    try {
        debitAccountStat = sqlConnection.prepareStatement(SQL_DEBIT_ACCOUNT);
        debitAccountStat.setBigDecimal(1, total);
        debitAccountStat.setInt(2, accountId);
        debitAccountStat.executeUpdate();

    } catch (SQLException e) {
        throw new DAOException("Excpetion is thrown when updating the account balance for accountID :"
                + accountId + " total :" + total, e);
    } finally {
        if (debitAccountStat != null) {
            try {
                debitAccountStat.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
}

From source file:nl.tudelft.stocktrader.mysql.MySQLCustomerDAO.java

public boolean insertAccount(Account accountBean) throws DAOException {
    PreparedStatement insertAccount = null;
    boolean insertSuccess = false;
    try {// www  .  j  av a  2  s  . c  o m
        insertAccount = sqlConnection.prepareStatement(SQL_INSERT_ACCOUNT);
        insertAccount.setBigDecimal(1, accountBean.getOpenBalance());
        insertAccount.setInt(2, accountBean.getLogoutCount());
        insertAccount.setBigDecimal(3, accountBean.getBalance());
        insertAccount.setDate(4, StockTraderUtility.convertToSqlDate(accountBean.getLastLogin()));
        insertAccount.setInt(5, accountBean.getLoginCount());
        insertAccount.setString(6, accountBean.getUserID());
        insertAccount.executeUpdate();
        insertSuccess = true;

    } catch (SQLException e) {
        insertSuccess = false;
        throw new DAOException("", e);

    } finally {
        if (insertAccount != null) {
            try {
                insertAccount.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
    return insertSuccess;
}