Handling Numbers


Handling Numbers

This section describes the issues associated with storing numbers in your Java programs. An Oracle database is capable of storing numbers with a precision of up to 38 digits. In the context of number representation, precision refers to the accuracy with which a floating-point number may be represented in a digital computer s memory. The 38 digits of precision offered by the database allows you to store very large numbers.

That s fine when working with numbers in the database, but as you have seen, Java uses its own set of types to represent numbers, so you must be careful when selecting the Java type that will be used to represent numbers in your programs, especially if those numbers are going to be stored in a database.

To store integers in your Java program, you can use the short , int , long , or java.math.BigInteger types, depending on how big the integer you want to store is and how much memory space you want to use. Table 15-4 shows the number of bits used to store short , int , and long types, along with the low and high values supported by each type:

Table 15-4: short, int, and long Types

Type

Bits

Low Value

High Value

short

16

“32768

32767

int

32

“2147483648

2147483647

long

64

“9223372036854775808

9223372036854775807

To store floating-point numbers in your Java programs, you can use the float , double , or java.math.BigDecimal types. Table 15-5 shows the same columns as Table 15-4 for the floa t and double types, along with the precision supported by each of these types:

Table 15-5: float and double Types

Type

Bits

Low Value

High Value

Precision

float

32

“3.4E+38

3.4E+38

6 digits

double

64

“1.7E+308

1.7E+308

15 digits

As you can see, a float may be used to store floating-point numbers with a precision of up to 6 digits, and a double may used for floating-point numbers with a precision of up to 15 digits. If you have a floating-point number that requires more than 15 digits of precision for storage in your Java program, you can use the java.math.BigDecimal type, which can store an arbitrarily long floating-point number.

In addition to these types, you can also use one of the Oracle JDBC extension types to store your integer or floating-point numbers. This type is oracle.sql.NUMBER , and allows you to store numbers with up to 38 digits of precision. You ll learn more about the oracle.sql.NUMBE R type later in this chapter.

Note  

For Oracle10 g , you can use the oracle.sql.BINARY _ FLOAT and oracle.sql.BINARY _ DOUBLE types. These types allow you to store the new Oracle10 g BINARY _ FLOAT and BINARY _ DOUBLE numbers.

Let s take a look at some examples of using these integer and floating-point types to store the product_id and price column values for a row retrieved from the products table. Assume that a ResultSet object named productResultSet has been populated with the product_id and price columns for a row from the products table. The product_id column is defined as a database INTEGER , and the price column is defined as a database NUMBER . The following example creates variables of the various integer and floating-point types and retrieves the product _id and price column values into those variables:

 short productIdShort = productResultSet.getShort("product_id"); int productIdInt = productResultSet.getInt("product_id"); long productIdLong = productResultSet.getLong("product_id"); float priceFloat = productResultSet.getFloat("price"); double priceDouble = productResultSet.getDouble("price"); java.math.BigDecimal priceBigDec = productResultSet.getBigDecimal("price"); 

Notice the use of the different get methods to retrieve the column values as the different types, the output of which is then stored in a Java variable of the appropriate type.




Oracle Database 10g SQL
Oracle Database 10g SQL (Osborne ORACLE Press Series)
ISBN: 0072229810
EAN: 2147483647
Year: 2004
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net