Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to know the Data-type of column?

 
Sangram Jo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic