The package java.sql contains a number of data types for compatibility with SQL database types. Some of these types are shown in Table.
Java format types
Although, not directly related to JDBC, the package java.text contains a number of classes which can be used for formatting output. The classes provided here include those to format java.util.Date and floating point numbers for display output.
Here is an example of using the class DateFormat to format a date for display purposes:
Calendar cal = Calendar.getInstance (TimeZone.getTimeZone (“AET”),
new Locale (“AU”, “EN”));
Date aDate = cal.getTime ();
DateFormat df = DateFormat.getDateTimeInstance ();
df.setTimeZone (TimeZone.getTimeZone (“AET”));
String date = df.format (aDate);
System.out.println (“Date is:” + date);
Note that in the versions of Java prior to Java 2, simply obtaining a java.util.Date object does not work, as there is a problem with localities outside the United States. Therefore, some of these methods have been deprecated, and the method Calendar is to be used instead.
Table Java and SQL data types.
Java Type | SQL Types | Description |
String | CHAR VARCHAR LONGVARCHAR | Single character String of characters of variable length. Very long strings of the variable length. |
int | INTEGER | 32-bit values |
Double | FLOAT DOUBLE | 15-bit mantissa precision |
byte [ ] | BINARY VARBINARY LONGVARBINARY | Binary data (array of binary values) ranging up to MultiMegabytes |
java.sql.Date | DATE | Date: yyyy-mm-dd |
java.sql.Time | TIME | Time: hh-mm-ss |
java.sql.Timestamp | TIMESTAMP | A more precise time |