JDBC DATA TYPES

JDBC specification provides definitions for all of the SQL data types that can be supported by a JDBC driver. Only a few of these data types may be natively supported by a given database system, which is why data coercion becomes such a vital service (we’ll discuss data coercion a little later in this chapter).
The data types are defined in Types.class:

public class Types
{
      public final static int BIT = -7;
      public final static int TINYINT = -6;
      public final static int SMALLINT = 5;
      public final static int INTEGER = 4;
      public final static int BIGINT = -5;
      public final static int FLOAT = 6;
      public final static int REAL = 7;
      public final static int DOUBLE = 8;
      public final static int NUMERIC = 2;
      public final static int DECIMAL = 3;
      public final static int CHAR = 1;
      public final static int VARCHAR = 12;
      public final static int LONGVARCHAR = -1;
      public final static int DATE = 91;
      public final static int TIME = 92;
      public final static int TIMESTAMP = 93;
      public final static int BINARY = -2;
      public final static int VARBINARY = -3;
      public final static int LONGVARBINARY = -4;
      public final static int OTHER = 1111;
}

Character Data:

 CHAR, VARCHAR, And LONGVARCHAR CHAR, VARCHAR, and LONGVARCHAR data types are used to express character data. These data types are represented in JDBC as Java String objects. Data of type CHAR is represented as a fixed-length String, and may include some padding spaces to ensure that it is the proper length. If data is being written to a database, the driver must ensure that the data is properly padded. Data of type VARCHAR is represented as a variable-length String, and is trimmed to the actual length of the data. LONGVARCHAR data can be either a variable-length String or returned by the driver as a Java InputStream, allowing the data to be read in chunks of whatever size the application desires.

At a minimum, a JDBC driver must support one (if not all) of the character data types (CHAR, VARCHAR, and LONGVARCHAR).A driver may also support driver-specific data types (OTHER) which can only be accessed in a JDBC application as an Object.In other words, you can get data as some type of object and put it back into a database as that same type of object, but the application has no idea what type of data is actually contained within.

Exact Numeric Data: 

NUMERIC And DECIMAL The NUMERIC and DECIMAL data types are used to express signed, exact numeric values with a fixed number of decimal places. These data types are often used to represent currency values. NUMERIC and DECIMAL data are both represented in JDBC as Numeric objects.

Binary Data:

 BINARY, VARBINARY, And LONGVARBINARY The BINARY, VARBINARY, and LONGVARBINARY data types are used to express binary (non-character) data. These data types are represented in JDBC as Java byte arrays. Data of type BINARY is represented as a fixed-length byte array, and may include some padding zeros to ensure that it is the proper length. If data is being written to a database, the driver must ensure that the data is properly padded. Data of type VARBINARY is represented as a variable-length byte array, and is trimmed to the actual length of the data. LONGVARBINARY data can either be a variable-length byte array or returned by the driver as a Java InputStream, allowing the data to be read in chunks of whatever size the application desires.

Boolean Data:

BIT The BIT data type is used to represent a boolean value—either true or false—and is represented in JDBC as a Boolean object or boolean data type.

Integer Data:

TINYINT, SMALLINT, INTEGER, And BIGINT The TINYINT, SMALLINT, INTEGER, and BIGINT data types are used to represent signed integer data. Data of type TINYINT is represented in JDBC as a Java byte data type (1 byte), with a minimum value of -128 and a maximum value of 127. Data of type SMALLINT is represented in JDBC as a Java short data type (2 bytes), with a minimum value of -32,768 and a maximum value of 32,767. Data of type INTEGER is represented as a Java int data type (4 bytes), with a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. Data of type BIGINT is represented as a Java long data type (8 bytes), with a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807.

Floating-Point Data: 

REAL, FLOAT, And DOUBLE The REAL, FLOAT, and DOUBLE data types are used to represent signed, approximate values. Data of type REAL supports seven digits of mantissa precision, and is represented as a Java float data type. Data of types FLOAT and DOUBLE support 15 digits of mantissa precision, and are represented as Java double data types.

Time Data: 

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. Numeric.

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.