Package java.math

Examples of java.math.BigDecimal


    private void testRow(Statement stat, String name) throws SQLException {
        ResultSet rs = stat.executeQuery("SELECT * FROM " + name + " WHERE ID=1");
        rs.next();
        assertEquals("Hello", rs.getString("NAME"));
        assertEquals(-1, rs.getByte("XT"));
        BigDecimal bd = rs.getBigDecimal("XD");
        assertTrue(bd.equals(new BigDecimal("10.30")));
        Timestamp ts = rs.getTimestamp("XTS");
        String s = ts.toString();
        assertEquals("2001-02-03 11:22:33.4455", s);
        assertTrue(ts.equals(Timestamp.valueOf("2001-02-03 11:22:33.4455")));
        assertEquals(new byte[] { (byte) 255, (byte) 1, (byte) 2 }, rs.getBytes("XBY"));
View Full Code Here


    private static List[] sampleBatch(int rows) {
        List[] batch = new List[rows];
       
        for (int i = 0; i < rows; i++) {
            long currentTime = System.currentTimeMillis();
            Object[] data = { new BigDecimal("" + i), //$NON-NLS-1$
                              new BigInteger(Integer.toString(i)),
                              (i%2 == 0) ? Boolean.FALSE: Boolean.TRUE,
                              new Byte((byte)i),
                              new Character((char)i),
                              new Date(currentTime),
View Full Code Here

        List[] batch = new List[rows];
       
        for (int i = 0; i < rows; i++) {
            long currentTime = System.currentTimeMillis();
            int mod = i%14;
            Object[] data = { (mod == 0) ? null : new BigDecimal("" + i), //$NON-NLS-1$
                              (mod == 1) ? null : new BigInteger(Integer.toString(i)),
                              (mod == 2) ? null : ((i%2 == 0) ? Boolean.FALSE: Boolean.TRUE),
                              (mod == 3) ? null : new Byte((byte)i),
                              (mod == 4) ? null : new Character((char)i),
                              (mod == 5) ? null : new Date(currentTime),
View Full Code Here

        assertEquals("INTEGER", meta.getColumnTypeName(1));
    }

    private void testArray(Connection conn) throws SQLException {
        PreparedStatement prep = conn.prepareStatement("select * from table(x int = ?) order by x");
        prep.setObject(1, new Object[] { new BigDecimal("1"), "2" });
        ResultSet rs = prep.executeQuery();
        rs.next();
        assertEquals("1", rs.getString(1));
        rs.next();
        assertEquals("2", rs.getString(1));
View Full Code Here

        stat.execute("INSERT INTO TEST VALUES(1, 'Hello')");
        PreparedStatement prep = conn
                .prepareStatement("SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM TEST");
        prep.setObject(1, new Boolean(true));
        prep.setObject(2, "Abc");
        prep.setObject(3, new BigDecimal("10.2"));
        prep.setObject(4, new Byte((byte) 0xff));
        prep.setObject(5, new Short(Short.MAX_VALUE));
        prep.setObject(6, new Integer(Integer.MIN_VALUE));
        prep.setObject(7, new Long(Long.MAX_VALUE));
        prep.setObject(8, new Float(Float.MAX_VALUE));
        prep.setObject(9, new Double(Double.MAX_VALUE));
        prep.setObject(10, java.sql.Date.valueOf("2001-02-03"));
        prep.setObject(11, java.sql.Time.valueOf("04:05:06"));
        prep.setObject(12, java.sql.Timestamp.valueOf("2001-02-03 04:05:06.123456789"));
        prep.setObject(13, new java.util.Date(java.sql.Date.valueOf("2001-02-03").getTime()));
        prep.setObject(14, new byte[] { 10, 20, 30 });
        prep.setObject(15, new Character('a'), Types.OTHER);
        prep.setObject(16, "2001-01-02", Types.DATE);
        // converting to null seems strange...
        prep.setObject(17, "2001-01-02", Types.NULL);
        prep.setObject(18, "3.725", Types.DOUBLE);
        prep.setObject(19, "23:22:21", Types.TIME);
        prep.setObject(20, new java.math.BigInteger("12345"), Types.OTHER);
        rs = prep.executeQuery();
        rs.next();
        assertTrue(rs.getObject(1).equals(new Boolean(true)));
        assertTrue(rs.getObject(2).equals("Abc"));
        assertTrue(rs.getObject(3).equals(new BigDecimal("10.2")));
        assertTrue(rs.getObject(4).equals((byte) 0xff));
        assertTrue(rs.getObject(5).equals(new Short(Short.MAX_VALUE)));
        assertTrue(rs.getObject(6).equals(new Integer(Integer.MIN_VALUE)));
        assertTrue(rs.getObject(7).equals(new Long(Long.MAX_VALUE)));
        assertTrue(rs.getObject(8).equals(new Float(Float.MAX_VALUE)));
View Full Code Here

     * @param value the value
     * @param scale the scale
     * @return the big decimal object
     */
    BigDecimal getBigDecimal(int value, int scale) {
        return new BigDecimal(new BigInteger(String.valueOf(value)), scale);
    }
View Full Code Here

        prep.setInt(1, dId);
        prep.setInt(2, warehouseId);
        rs = db.query(prep);
        rs.next();
        int oId = rs.getInt(1) - 1;
        BigDecimal tax = rs.getBigDecimal(2);
        rs.close();
        prep = prepare("SELECT C_DISCOUNT, C_LAST, C_CREDIT, W_TAX "
                + "FROM CUSTOMER, WAREHOUSE "
                + "WHERE C_ID=? AND W_ID=? AND C_W_ID=W_ID AND C_D_ID=?");
        prep.setInt(1, cId);
        prep.setInt(2, warehouseId);
        prep.setInt(3, dId);
        rs = db.query(prep);
        rs.next();
        BigDecimal discount = rs.getBigDecimal(1);
        // c_last
        rs.getString(2);
        // c_credit
        rs.getString(3);
        BigDecimal wTax = rs.getBigDecimal(4);
        rs.close();
        BigDecimal total = new BigDecimal("0");
        for (int number = 1; number <= olCnt; number++) {
            int olId = itemId[number - 1];
            int olSupplyId = supplyId[number - 1];
            int olQuantity = quantity[number - 1];
            prep = prepare("SELECT I_PRICE, I_NAME, I_DATA "
                    + "FROM ITEM WHERE I_ID=?");
            prep.setInt(1, olId);
            rs = db.query(prep);
            if (!rs.next()) {
                if (rollback) {
                    // item not found - correct behavior
                    db.rollback();
                    return;
                }
                throw new SQLException("item not found: " + olId + " "
                        + olSupplyId);
            }
            BigDecimal price = rs.getBigDecimal(1);
            // i_name
            rs.getString(2);
            String data = rs.getString(3);
            rs.close();
            prep = prepare("SELECT S_QUANTITY, S_DATA, "
                    + "S_DIST_01, S_DIST_02, S_DIST_03, S_DIST_04, S_DIST_05, "
                    + "S_DIST_06, S_DIST_07, S_DIST_08, S_DIST_09, S_DIST_10 "
                    + "FROM STOCK WHERE S_I_ID=? AND S_W_ID=?");
            prep.setInt(1, olId);
            prep.setInt(2, olSupplyId);
            rs = db.query(prep);
            if (!rs.next()) {
                if (rollback) {
                    // item not found - correct behavior
                    db.rollback();
                    return;
                }
                throw new SQLException("item not found: " + olId + " "
                        + olSupplyId);
            }
            int sQuantity = rs.getInt(1);
            String sData = rs.getString(2);
            String[] dist = new String[10];
            for (int i = 0; i < 10; i++) {
                dist[i] = rs.getString(3 + i);
            }
            rs.close();
            String distInfo = dist[dId - 1];
            stock[number - 1] = sQuantity;
            if ((data.indexOf("original") != -1)
                    && (sData.indexOf("original") != -1)) {
                bg[number - 1] = 'B';
            } else {
                bg[number - 1] = 'G';
            }
            if (sQuantity > olQuantity) {
                sQuantity = sQuantity - olQuantity;
            } else {
                sQuantity = sQuantity - olQuantity + 91;
            }
            prep = prepare("UPDATE STOCK SET S_QUANTITY=? "
                    + "WHERE S_W_ID=? AND S_I_ID=?");
            prep.setInt(1, sQuantity);
            prep.setInt(2, olSupplyId);
            prep.setInt(3, olId);
            db.update(prep, "updateStock");
            BigDecimal olAmount = new BigDecimal(olQuantity).multiply(
                    price).multiply(ONE.add(wTax).add(tax)).multiply(
                    ONE.subtract(discount));
            olAmount = olAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
            amt[number - 1] = olAmount;
            total = total.add(olAmount);
            prep = prepare("INSERT INTO ORDER_LINE (OL_O_ID, OL_D_ID, OL_W_ID, OL_NUMBER, "
                    + "OL_I_ID, OL_SUPPLY_W_ID, "
                    + "OL_QUANTITY, OL_AMOUNT, OL_DIST_INFO) "
View Full Code Here

        } else {
            byName = false;
            last = "";
            cId = random.getNonUniform(1023, 1, bench.customersPerDistrict);
        }
        BigDecimal amount = random.getBigDecimal(random.getInt(100, 500000),
                2);
        Timestamp datetime = new Timestamp(System.currentTimeMillis());
        PreparedStatement prep;
        ResultSet rs;

        prep = prepare("UPDATE DISTRICT SET D_YTD = D_YTD+? "
                + "WHERE D_ID=? AND D_W_ID=?");
        prep.setBigDecimal(1, amount);
        prep.setInt(2, dId);
        prep.setInt(3, warehouseId);
        db.update(prep, "updateDistrict");
        prep = prepare("UPDATE WAREHOUSE SET W_YTD=W_YTD+? WHERE W_ID=?");
        prep.setBigDecimal(1, amount);
        prep.setInt(2, warehouseId);
        db.update(prep, "updateWarehouse");
        prep = prepare("SELECT W_STREET_1, W_STREET_2, W_CITY, W_STATE, W_ZIP, W_NAME "
                + "FROM WAREHOUSE WHERE W_ID=?");
        prep.setInt(1, warehouseId);
        rs = db.query(prep);
        rs.next();
        // w_street_1
        rs.getString(1);
        // w_street_2
        rs.getString(2);
        // w_city
        rs.getString(3);
        // w_state
        rs.getString(4);
        // w_zip
        rs.getString(5);
        String wName = rs.getString(6);
        rs.close();
        prep = prepare("SELECT D_STREET_1, D_STREET_2, D_CITY, D_STATE, D_ZIP, D_NAME "
                + "FROM DISTRICT WHERE D_ID=? AND D_W_ID=?");
        prep.setInt(1, dId);
        prep.setInt(2, warehouseId);
        rs = db.query(prep);
        rs.next();
        // d_street_1
        rs.getString(1);
        // d_street_2
        rs.getString(2);
        // d_city
        rs.getString(3);
        // d_state
        rs.getString(4);
        // d_zip
        rs.getString(5);
        String dName = rs.getString(6);
        rs.close();
        BigDecimal balance;
        String credit;
        if (byName) {
            prep = prepare("SELECT COUNT(C_ID) FROM CUSTOMER "
                    + "WHERE C_LAST=? AND C_D_ID=? AND C_W_ID=?");
            prep.setString(1, last);
            prep.setInt(2, cdId);
            prep.setInt(3, wId);
            rs = db.query(prep);
            rs.next();
            int namecnt = rs.getInt(1);
            rs.close();
            if (namecnt == 0) {
                // TODO TPC-C: check if this can happen
                db.rollback();
                return;
            }
            prep = prepare("SELECT C_FIRST, C_MIDDLE, C_ID, "
                    + "C_STREET_1, C_STREET_2, C_CITY, C_STATE, C_ZIP, "
                    + "C_PHONE, C_CREDIT, C_CREDIT_LIM, "
                    + "C_DISCOUNT, C_BALANCE, C_SINCE FROM CUSTOMER "
                    + "WHERE C_LAST=? AND C_D_ID=? AND C_W_ID=? "
                    + "ORDER BY C_FIRST");
            prep.setString(1, last);
            prep.setInt(2, cdId);
            prep.setInt(3, wId);
            rs = db.query(prep);
            // locate midpoint customer
            if (namecnt % 2 != 0) {
                namecnt++;
            }
            for (int n = 0; n < namecnt / 2; n++) {
                rs.next();
            }
            // c_first
            rs.getString(1);
            // c_middle
            rs.getString(2);
            cId = rs.getInt(3);
            // c_street_1
            rs.getString(4);
            // c_street_2
            rs.getString(5);
            // c_city
            rs.getString(6);
            // c_state
            rs.getString(7);
            // c_zip
            rs.getString(8);
            // c_phone
            rs.getString(9);
            credit = rs.getString(10);
            // c_credit_lim
            rs.getString(11);
            // c_discount
            rs.getBigDecimal(12);
            balance = rs.getBigDecimal(13);
            // c_since
            rs.getTimestamp(14);
            rs.close();
        } else {
            prep = prepare("SELECT C_FIRST, C_MIDDLE, C_LAST, "
                    + "C_STREET_1, C_STREET_2, C_CITY, C_STATE, C_ZIP, "
                    + "C_PHONE, C_CREDIT, C_CREDIT_LIM, "
                    + "C_DISCOUNT, C_BALANCE, C_SINCE FROM CUSTOMER "
                    + "WHERE C_ID=? AND C_D_ID=? AND C_W_ID=?");
            prep.setInt(1, cId);
            prep.setInt(2, cdId);
            prep.setInt(3, wId);
            rs = db.query(prep);
            rs.next();
            // c_first
            rs.getString(1);
            // c_middle
            rs.getString(2);
            // c_last
            rs.getString(3);
            // c_street_1
            rs.getString(4);
            // c_street_2
            rs.getString(5);
            // c_city
            rs.getString(6);
            // c_state
            rs.getString(7);
            // c_zip
            rs.getString(8);
            // c_phone
            rs.getString(9);
            credit = rs.getString(10);
            // c_credit_lim
            rs.getString(11);
            // c_discount
            rs.getBigDecimal(12);
            balance = rs.getBigDecimal(13);
            // c_since
            rs.getTimestamp(14);
            rs.close();
        }
        balance = balance.add(amount);
        if (credit.equals("BC")) {
            prep = prepare("SELECT C_DATA INTO FROM CUSTOMER "
                    + "WHERE C_ID=? AND C_D_ID=? AND C_W_ID=?");
            prep.setInt(1, cId);
            prep.setInt(2, cdId);
View Full Code Here

                prep.setInt(1, noId);
                prep.setInt(2, dId);
                prep.setInt(3, warehouseId);
                rs = db.query(prep);
                rs.next();
                BigDecimal amount = rs.getBigDecimal(1);
                rs.close();
                prep = prepare("UPDATE CUSTOMER SET C_BALANCE=C_BALANCE+? "
                        + "WHERE C_ID=? AND C_D_ID=? AND C_W_ID=?");
                prep.setBigDecimal(1, amount);
                prep.setInt(2, noId);
View Full Code Here

    static ComplexObject build(Integer id, boolean isNull) {
        ComplexObject obj = new ComplexObject();
        obj.id = id;
        obj.amount = isNull ? null : Long.valueOf(1);
        obj.name = isNull ? null : "hello";
        obj.value = isNull ? null : new BigDecimal("1");
        obj.birthday = isNull ? null : java.sql.Date.valueOf("2001-01-01");
        obj.time = isNull ? null : Time.valueOf("10:20:30");
        obj.created = isNull ? null : Timestamp.valueOf("2002-02-02 02:02:02");
        return obj;
    }
View Full Code Here

TOP

Related Classes of java.math.BigDecimal

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.