by Dinesh Thakur Category: Servlet with MySql Database

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

 





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles