File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to know the Data-type of column? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "How to know the Data-type of column?" Watch "How to know the Data-type of column?" New topic
Author

How to know the Data-type of column?

Sangram Jo
Greenhorn

Joined: May 24, 2005
Posts: 3
Hi Everybody

We are writing a generalized function to display data in a tabular form fetched from DB. Almost function we've written. But now we are facing a problem with columns having Date as data-type. We want Date in DD/MM/YY & it is giving in YYYY/MM/DD format. So is their any way to know the Data type of column from ResultSet?. Because we are passing ResultSet to the function.

Thanking You
Sangram Jo
Greenhorn

Joined: May 24, 2005
Posts: 3
Hi

I got answer for the same.

rs.getColumnType(colIndex) returns data type of column.
like
BIT
TINYINT
BIGINT
LONGVARBINARY
VARBINARY
BINARY
LONGVARCHAR
NULL
CHAR
NUMERIC
DECIMAL
INTEGER
SMALLINT
FLOAT
REAL
DOUBLE
VARCHAR
DATE
TIME
TIMESTAMP
OTHER

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Assuming you handle date data types as Date objects in your JDBC code and PreparedStatements rather than Statements then the format of the date doesn't matter and you won't need to check the column type (which is going to add lots of unnecesary conditional code to support every possible "date" data type across all databases).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26144
    
  66

Sangram,
Take a look at SimpleDateFormat for the actual date display. Usually I would recommend against using metadata, but your function is generic and needs it. The conditional logic shouldn't be too bad though. If the field is a Date/Time/TimeStamp, then you can call getDate() and use SimpleDateFormat. This minimizes your dependence on the specific database as much as possible.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1112

Sangram,
From the information you have supplied, it looks like you are using the "toString()" method of class "java.sql.Date" to display date values retrieved from the database. Note that you may not be doing this directly. If you are displaying the value in a "JTable", then the "JTable" is doing this for you.

The "toString()" method of "java.sql.Date" does, in fact, display dates in "YYYY/MM/DD" format.

As Jeanne suggests, you can use the "java.text.SimpleDateFormat" to change the format. But in any case, if you are using a "JTable", you may need to override its behaviour. You may need to make a custom cell renderer, or a custom "TableModel" or a custom "ColumnModel".

Good Luck,
Avi.
 
I agree. Here's the link: http://jrebel.com/download
 
subject: How to know the Data-type of column?
 
Similar Threads
how to map Date
Date Formatting
oracle DATE
user-defined date format in XML schema
insert and get date in postgresql