Time Data: DATE, TIME, And TIMESTAMP The DATE, TIME, and TIMESTAMP data types are used to represent dates and times. Data of type DATE supports specification of the month, day, and year, and is represented as a JDBC Date object. Data of type TIME supports specification of the hour, minutes, seconds, and milliseconds, and is represented as a JDBC Time object. Data of type TIMESTAMP supports specification of the month, day, year, hour, minutes, seconds, and milliseconds, and is represented as a JDBC Timestamp object. The Date, Time, and Timestamp objects, which we ll get into a bit later, are new with JDBC. Tip: Be aware of date limitations. One important note about Date and Timestamp objects: The Java calendar starts at January 1, 1970, which means that you cannot represent dates prior to 1970. New Data Classes The JDBC API introduced several new data classes. These classes were developed to solve specific data-representation problems like how to accurately represent fixed- precision numeric values (such as currency values) for NUMERIC and DECIMAL data types, and how to represent time data for DATE, TIME, and TIMESTAMP data types. Numeric As mentioned before, the Numeric class was introduced with the JDBC API to represent signed, exact numeric values with a fixed number of decimal places. This class is ideal for representing monetary values, allowing accurate arithmetic operations and comparisons. Another aspect is the ability to change the rounding value. Rounding is performed if the value of the scale (the number of fixed decimal places) plus one digit to the right of the decimal point is greater than the rounding value. By default, the rounding value is 4. For example, if the result of an arithmetic operation is 2.495, and the scale is 2, the number is rounded to 2.50. Listing 10.6 provides an example of changing the rounding value. Imagine that you are a devious retailer investigating ways to maximize your profit by adjusting the rounding value. Listing 10.6 Changing the rounding value. import java.sql.*; class NumericRoundingValueTest { public static void main(String args[]) { // Set our price and discount amounts Numeric price = new Numeric(4.91, 2); Numeric discount = new Numeric(0.15, 2); Numeric newPrice; // Give the item a discount newPrice = discountItem(price, discount);
We provides quality the CPanel Web Hosting. All our web hosting plans regular, business and expert are competitively priced and unsurpassed in reliability, uptime, and quality of service.