Example usage for java.sql ResultSet getBigDecimal

List of usage examples for java.sql ResultSet getBigDecimal

Introduction

In this page you can find the example usage for java.sql ResultSet getBigDecimal.

Prototype

BigDecimal getBigDecimal(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a java.math.BigDecimal with full precision.

Usage

From source file:com.alibaba.wasp.jdbc.TestPreparedStatement.java

private void checkBigDecimal(ResultSet rs, String[] value) throws SQLException {
    for (String v : value) {
        assertTrue(rs.next());//www  .j a v  a 2 s .c o  m
        BigDecimal x = rs.getBigDecimal(1);
        trace("v=" + v + " x=" + x);
        if (v == null) {
            assertTrue(x == null);
        } else {
            assertTrue(x.compareTo(new BigDecimal(v)) == 0);
        }
    }
    assertTrue(!rs.next());
}

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

public Wallet getWallet(String userID) throws DAOException {
    PreparedStatement selectWallet = null;
    try {/*from  w ww. jav a2 s .  co  m*/
        selectWallet = sqlConnection.prepareStatement(SQL_SELECT_WALLET);
        selectWallet.setString(1, userID);
        ResultSet rs = selectWallet.executeQuery();
        try {
            while (rs.next()) {
                Wallet wallet = new Wallet(rs.getString(1), rs.getBigDecimal(2), rs.getBigDecimal(3),
                        rs.getBigDecimal(4), rs.getBigDecimal(5), rs.getBigDecimal(6));
                return wallet;
            }
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    } catch (SQLException e) {
        throw new DAOException("", e);
    } finally {
        if (selectWallet != null) {
            try {
                selectWallet.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
    return null;
}

From source file:org.kawanfw.test.api.client.InsertAndUpdateBlobTestNew.java

/**
 * Test that the blob was were correctly inserted
 * //from   w ww. ja  va2s. co m
 * @param connection
 */
public void selectGetBlobSyntax(Connection connection, String originalFileName, String shaHexa)
        throws Exception {
    int customer_id;
    int item_id;
    String description;
    BigDecimal cost_price;
    Date date_placed;
    Timestamp date_shipped;
    Blob blob;
    boolean is_delivered;
    int quantity;

    String sql = "select * from orderlog where  customer_id >= ? and item_id >= ? ";

    PreparedStatement prepStatement = connection.prepareStatement(sql);

    int i = 1;
    prepStatement.setInt(i++, 1);
    prepStatement.setInt(i++, 1);

    ResultSet rs = prepStatement.executeQuery();

    MessageDisplayer.display("");

    InputStream in = null;
    OutputStream out = null;

    SqlUtil sqlUtil = new SqlUtil(connection);

    while (rs.next()) {

        customer_id = rs.getInt("customer_id");
        item_id = rs.getInt("item_id");
        description = rs.getString("description");
        cost_price = rs.getBigDecimal("cost_price");
        date_placed = rs.getDate("date_placed");
        date_shipped = rs.getTimestamp("date_shipped");

        blob = rs.getBlob("jpeg_image");

        if (sqlUtil.isIngres()) {
            is_delivered = (rs.getInt("is_delivered") == 1) ? true : false;
        } else {
            is_delivered = rs.getBoolean("is_delivered");
        }

        quantity = rs.getInt("quantity");

        i = 1;
        customer_id = rs.getInt(i++);
        item_id = rs.getInt(i++);
        description = rs.getString(i++);
        cost_price = rs.getBigDecimal(i++);
        date_placed = rs.getDate(i++);
        date_shipped = rs.getTimestamp(i++);

        File originalBlobFile = SqlTestParms.getFileFromUserHome(originalFileName);

        File file = createTempFile(originalBlobFile.toString());

        try {
            in = blob.getBinaryStream();

            if (in != null) {
                out = new BufferedOutputStream(new FileOutputStream(file));
                IOUtils.copy(in, out);
            } else {
                MessageDisplayer.display("jpeg_image column is null!");
            }

        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
            try {
                blob.free();
            } catch (Throwable e) {
                MessageDisplayer.display("blob.free() not done: " + e.toString());
            }
        }

        i++;
        if (sqlUtil.isIngres()) {
            is_delivered = (rs.getInt(i++) == 1) ? true : false;
        } else {
            is_delivered = rs.getBoolean(i++);
        }

        quantity = rs.getInt(i++);

        MessageDisplayer.display("");
        MessageDisplayer.display("customer_id : " + customer_id);
        MessageDisplayer.display("item_id     : " + item_id);
        MessageDisplayer.display("description : " + description);
        MessageDisplayer.display("cost_price  : " + cost_price);
        MessageDisplayer.display("date_placed : " + date_placed);
        MessageDisplayer.display("date_shipped: " + date_shipped);
        MessageDisplayer.display("jpeg_image  : " + "content stored in file: " + file);
        MessageDisplayer.display("is_delivered: " + is_delivered);
        MessageDisplayer.display("quantity    : " + quantity);

        // Compute the hash of the file
        Sha1Util sha1 = new Sha1Util();
        String shaHexaNew = sha1.getHexFileHash(file);

        Assert.assertEquals(shaHexa, shaHexaNew);

        file.delete();

        MessageDisplayer.display("");
        MessageDisplayer.display("Ok, SHA-1 value of read file " + file + " is same as inserted file "
                + SqlTestParms.getFileFromUserHome(originalFileName));

    }

    prepStatement.close();
    rs.close();

    MessageDisplayer.display("Select done!");

}

From source file:org.apache.phoenix.end2end.CursorWithRowValueConstructorIT.java

@Test
public void testCursorsWithColsOfTypesDecimal() throws Exception {
    long ts = nextTimestamp();
    String tenantId = getOrganizationId();
    initATableValues(ATABLE_NAME, tenantId, getDefaultSplits(tenantId), null, ts - 1, getUrl(), null);
    ensureTableCreated(getUrl(), CUSTOM_ENTITY_DATA_FULL_NAME, CUSTOM_ENTITY_DATA_FULL_NAME, ts - 1);
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);

    String query = "SELECT x_decimal FROM aTable WHERE ?=organization_id AND entity_id IN (?,?,?)";
    query = query.replaceFirst("\\?", "'" + tenantId + "'");
    query = query.replaceFirst("\\?", "'" + ROW7 + "'");
    query = query.replaceFirst("\\?", "'" + ROW8 + "'");
    query = query.replaceFirst("\\?", "'" + ROW9 + "'");

    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 2)); // Execute at timestamp 2
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {//from   w ww  .  j  a  v a 2  s .c  om
        String cursor = "DECLARE testCursor CURSOR FOR " + query;
        conn.prepareStatement(cursor).execute();
        cursor = "OPEN testCursor";
        conn.prepareStatement(cursor).execute();
        cursor = "FETCH NEXT FROM testCursor";

        ResultSet rs = conn.prepareStatement(cursor).executeQuery();
        int count = 0;
        while (rs.next()) {
            assertTrue(BigDecimal.valueOf(0.1).equals(rs.getBigDecimal(1))
                    || BigDecimal.valueOf(3.9).equals(rs.getBigDecimal(1))
                    || BigDecimal.valueOf(3.3).equals(rs.getBigDecimal(1)));
            count++;
            if (count == 3)
                break;
            rs = conn.prepareStatement(cursor).executeQuery();
        }
        assertTrue(count == 3);
    } finally {
        String sql = "CLOSE testCursor";
        conn.prepareStatement(sql).execute();
        conn.close();
    }
}

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

public Holding getHoldingForUpdate(int orderId) throws DAOException {
    if (logger.isDebugEnabled()) {
        logger.debug("MySQLCustomerDAO.getHoldingForUpdate(int)\nOrder ID :" + orderId);
    }//w  w  w.  j  av  a 2s .co  m

    Holding holding = null;
    PreparedStatement selectHoldingLockStat = null;
    try {
        selectHoldingLockStat = sqlConnection.prepareStatement(SQL_SELECT_HOLDING_LOCK);
        selectHoldingLockStat.setInt(1, orderId);
        ResultSet rs = selectHoldingLockStat.executeQuery();
        if (rs.next()) {
            try {
                holding = new Holding(rs.getInt(1), rs.getInt(2), rs.getDouble(3), rs.getBigDecimal(4),
                        StockTraderUtility.convertToCalendar(rs.getDate(5)), rs.getString(6));
                return holding;
            } finally {
                try {
                    rs.close();
                } catch (SQLException e) {
                    logger.debug("", e);
                }
            }
        }
    } catch (SQLException e) {
        throw new DAOException("Exception is thrown when selecting the holding entry for order ID :" + orderId,
                e);
    } finally {
        if (selectHoldingLockStat != null) {
            try {
                selectHoldingLockStat.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
    return holding;
}

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

public Holding getHolding(String userId, int holdingID) throws DAOException {
    if (logger.isDebugEnabled()) {
        logger.debug("MSSQLCustomerDAO.getHolding(String,int)\nUserID :" + userId + "\nOrder ID :" + holdingID);
    }/*from  www .jav  a  2 s .co  m*/
    Holding holding = null;
    PreparedStatement selectHoldingNoLockStat = null;
    try {
        selectHoldingNoLockStat = sqlConnection.prepareStatement(SQL_SELECT_HOLDING_NOLOCK);
        selectHoldingNoLockStat.setInt(1, holdingID);
        selectHoldingNoLockStat.setString(2, userId);

        ResultSet rs = selectHoldingNoLockStat.executeQuery();
        if (rs.next()) {
            try {
                holding = new Holding(rs.getInt(1), holdingID, rs.getDouble(2), rs.getBigDecimal(3),
                        StockTraderUtility.convertToCalendar(rs.getDate(4)), rs.getString(5));
                return holding;
            } finally {
                try {
                    rs.close();
                } catch (SQLException e) {
                    logger.debug("", e);
                }
            }
        }
    } catch (SQLException e) {
        logger.debug("", e);
        throw new DAOException("Exception is thrown when selecting the holding entry for userID :" + userId
                + " and orderID :" + holdingID, e);

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

From source file:org.geowebcache.diskquota.jdbc.JDBCQuotaStore.java

protected Quota getUsedQuotaByTileSetIdInternal(final String tileSetId) {
    String sql = dialect.getUsedQuotaByTileSetId(schema, "key");
    return jt.queryForOptionalObject(sql, new ParameterizedRowMapper<Quota>() {

        public Quota mapRow(ResultSet rs, int rowNum) throws SQLException {
            BigDecimal bytes = rs.getBigDecimal(1);
            Quota quota = new Quota(bytes.toBigInteger());
            quota.setTileSetId(tileSetId);
            return quota;
        }//w  w  w  . j a v a  2 s.c  o  m
    }, Collections.singletonMap("key", tileSetId));
}

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

public List<Holding> getHoldings(String userID) throws DAOException {
    PreparedStatement selectHoldings = null;
    try {//from  w  w  w.ja  v a 2 s . co m
        selectHoldings = sqlConnection.prepareStatement(SQL_SELECT_HOLDINGS);
        selectHoldings.setString(1, userID);
        ResultSet rs = selectHoldings.executeQuery();
        List<Holding> holdings = new ArrayList<Holding>();
        try {
            while (rs.next()) {
                Holding holding = new Holding(rs.getInt(1), rs.getDouble(2), rs.getBigDecimal(3),
                        StockTraderUtility.convertToCalendar(rs.getDate(4)), rs.getString(5), rs.getInt(6));
                holdings.add(holding);
            }
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
        return holdings;
    } catch (SQLException e) {
        throw new DAOException("", e);
    } finally {
        if (selectHoldings != null) {
            try {
                selectHoldings.close();
            } catch (SQLException e) {
                logger.debug("", e);
            }
        }
    }
}

From source file:org.kawanfw.test.api.client.InsertAndUpdateBlobTest.java

/**
 * Test that the blob was were correctly inserted
 * //from w  w w. j  a  v a  2 s  .  c  o m
 * @param connection
 */
public void selectBlobTestAlternateSyntax(Connection connection, String originalFileName, String shaHexa)
        throws Exception {
    int customer_id;
    int item_id;
    String description;
    BigDecimal cost_price;
    Date date_placed;
    Timestamp date_shipped;
    Blob blob;
    boolean is_delivered;
    int quantity;

    String sql = "select * from orderlog where  customer_id >= ? and item_id >= ? ";

    PreparedStatement prepStatement = connection.prepareStatement(sql);

    int i = 1;
    prepStatement.setInt(i++, 1);
    prepStatement.setInt(i++, 1);

    ResultSet rs = prepStatement.executeQuery();

    MessageDisplayer.display("");

    InputStream in = null;
    OutputStream out = null;

    SqlUtil sqlUtil = new SqlUtil(connection);

    while (rs.next()) {

        customer_id = rs.getInt("customer_id");
        item_id = rs.getInt("item_id");
        description = rs.getString("description");
        cost_price = rs.getBigDecimal("cost_price");
        date_placed = rs.getDate("date_placed");
        date_shipped = rs.getTimestamp("date_shipped");
        blob = rs.getBlob("jpeg_image");

        if (sqlUtil.isIngres()) {
            is_delivered = (rs.getInt("is_delivered") == 1) ? true : false;
        } else {
            is_delivered = rs.getBoolean("is_delivered");
        }

        quantity = rs.getInt("quantity");

        i = 1;
        customer_id = rs.getInt(i++);
        item_id = rs.getInt(i++);
        description = rs.getString(i++);
        cost_price = rs.getBigDecimal(i++);
        date_placed = rs.getDate(i++);
        date_shipped = rs.getTimestamp(i++);

        File originalBlobFile = SqlTestParms.getFileFromUserHome(originalFileName);
        // String extension = "."
        // + StringUtils.substringAfterLast(
        // originalBlobFile.toString(), ".");

        File file = createTempFile(originalBlobFile.toString());

        try {
            in = blob.getBinaryStream();

            if (in != null) {
                out = new BufferedOutputStream(new FileOutputStream(file));
                IOUtils.copy(in, out);
            } else {
                MessageDisplayer.display("jpeg_image column is null!");
            }

        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
            try {
                blob.free();
            } catch (Throwable e) {
                MessageDisplayer.display("blob.free() not done: " + e.toString());
            }
        }

        i++;
        if (sqlUtil.isIngres()) {
            is_delivered = (rs.getInt(i++) == 1) ? true : false;
        } else {
            is_delivered = rs.getBoolean(i++);
        }

        quantity = rs.getInt(i++);

        MessageDisplayer.display("");
        MessageDisplayer.display("customer_id : " + customer_id);
        MessageDisplayer.display("item_id     : " + item_id);
        MessageDisplayer.display("description : " + description);
        MessageDisplayer.display("cost_price  : " + cost_price);
        MessageDisplayer.display("date_placed : " + date_placed);
        MessageDisplayer.display("date_shipped: " + date_shipped);
        MessageDisplayer.display("jpeg_image  : " + "content stored in file: " + file);
        MessageDisplayer.display("is_delivered: " + is_delivered);
        MessageDisplayer.display("quantity    : " + quantity);

        // Compute the hash of the file
        Sha1Util sha1 = new Sha1Util();
        String shaHexaNew = sha1.getHexFileHash(file);

        Assert.assertEquals(shaHexa, shaHexaNew);

        file.delete();

        MessageDisplayer.display("");
        MessageDisplayer.display("Ok, SHA-1 value of read file " + file + " is same as inserted file "
                + SqlTestParms.getFileFromUserHome(originalFileName));

    }

    prepStatement.close();
    rs.close();

    MessageDisplayer.display("Select done!");

}

From source file:org.kawanfw.test.api.client.InsertAndUpdateBlobTestPsqlOID.java

/**
 * Test that the blob was were correctly inserted
 * /*from w  ww.ja v a2s . c  o  m*/
 * @param connection
 */
public void selectBlobTestAlternateSyntax(Connection connection, String originalFileName, String shaHexa)
        throws Exception {
    int customer_id;
    int item_id;
    String description;
    BigDecimal cost_price;
    Date date_placed;
    Timestamp date_shipped;
    Blob blob;
    boolean is_delivered;
    int quantity;

    String sql = "select * from orderlog_2 where  customer_id >= ? and item_id >= ? ";

    PreparedStatement prepStatement = connection.prepareStatement(sql);

    int i = 1;
    prepStatement.setInt(i++, 1);
    prepStatement.setInt(i++, 1);

    ResultSet rs = prepStatement.executeQuery();

    MessageDisplayer.display("");

    InputStream in = null;
    OutputStream out = null;

    SqlUtil sqlUtil = new SqlUtil(connection);

    while (rs.next()) {

        customer_id = rs.getInt("customer_id");
        item_id = rs.getInt("item_id");
        description = rs.getString("description");
        cost_price = rs.getBigDecimal("cost_price");
        date_placed = rs.getDate("date_placed");
        date_shipped = rs.getTimestamp("date_shipped");
        blob = rs.getBlob("jpeg_image");

        if (sqlUtil.isIngres()) {
            is_delivered = (rs.getInt("is_delivered") == 1) ? true : false;
        } else {
            is_delivered = rs.getBoolean("is_delivered");
        }

        quantity = rs.getInt("quantity");

        i = 1;
        customer_id = rs.getInt(i++);
        item_id = rs.getInt(i++);
        description = rs.getString(i++);
        cost_price = rs.getBigDecimal(i++);
        date_placed = rs.getDate(i++);
        date_shipped = rs.getTimestamp(i++);

        File originalBlobFile = SqlTestParms.getFileFromUserHome(originalFileName);
        // String extension = "."
        // + StringUtils.substringAfterLast(
        // originalBlobFile.toString(), ".");

        File file = createTempFile(originalBlobFile.toString());

        try {
            in = blob.getBinaryStream();

            if (in != null) {
                out = new BufferedOutputStream(new FileOutputStream(file));
                IOUtils.copy(in, out);
            } else {
                MessageDisplayer.display("jpeg_image column is null!");
            }

        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
            try {
                blob.free();
            } catch (Throwable e) {
                MessageDisplayer.display("blob.free() not done: " + e.toString());
            }
        }

        i++;
        if (sqlUtil.isIngres()) {
            is_delivered = (rs.getInt(i++) == 1) ? true : false;
        } else {
            is_delivered = rs.getBoolean(i++);
        }

        quantity = rs.getInt(i++);

        MessageDisplayer.display("");
        MessageDisplayer.display("customer_id : " + customer_id);
        MessageDisplayer.display("item_id     : " + item_id);
        MessageDisplayer.display("description : " + description);
        MessageDisplayer.display("cost_price  : " + cost_price);
        MessageDisplayer.display("date_placed : " + date_placed);
        MessageDisplayer.display("date_shipped: " + date_shipped);
        MessageDisplayer.display("jpeg_image  : " + "content stored in file: " + file);
        MessageDisplayer.display("is_delivered: " + is_delivered);
        MessageDisplayer.display("quantity    : " + quantity);

        // Compute the hash of the file
        Sha1Util sha1 = new Sha1Util();
        String shaHexaNew = sha1.getHexFileHash(file);

        Assert.assertEquals(shaHexa, shaHexaNew);

        file.delete();

        MessageDisplayer.display("");
        MessageDisplayer.display("Ok, SHA-1 value of read file " + file + " is same as inserted file "
                + SqlTestParms.getFileFromUserHome(originalFileName));

    }

    prepStatement.close();
    rs.close();

    MessageDisplayer.display("Select done!");

}