Equivalent Java data type to hold the Informix DATETIME YEAR TO SECOND value?
I wanted to map the Informix DATETIME YEAR TO SECOND to a Java data type.
Can someone give the corresponding Java data type to hold the Informix DATET开发者_运维技巧IME YEAR TO SECOND value?
This is the answer: java.sql.Time according to my JDBC driver.
DATETIME YEAR TO SECOND can be mapped to a TIMESTAMP according to this documentation :
http://publib.boulder.ibm.com/infocenter/iwedhelp/v6r0/index.jsp?topic=/com.ibm.db2e.doc/dbsap_b3.html
TIMESTAMPs can be mapped to java.util.Date.
If you need to perform calculation on your Date, use java.util.Calendar:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// Use the roll fonction to alter the date
// For example add one month to your Date
calendar.roll(Calendar.MONTH, 1);
// Get your Date back
date = calendar.getTime();
Do you use any framework to map your data ?
I made simple test using table from Jonathan Leffler answer to: Informix 7.3 - Declaring a date column data type with default as current date on insert/update
I use Jython and JDBC Informix driver JDBC.3.70.JC1DE. Test code that uses metadata looks like:
db = DriverManager.getConnection(db_url, usr, passwd)
c = db.createStatement()
rs = c.executeQuery("SELECT * FROM test_datetime")
rsmd = rs.getMetaData()
print('columnCnt: %d' % (rsmd.getColumnCount()))
while (rs.next()):
for i in range(rsmd.getColumnCount()):
col_no = i + 1
print("value: [%s]" % rs.getString(col_no))
print("name: [%s]" % rsmd.getColumnName(col_no))
print("Java type: %d" % rsmd.getColumnType(col_no))
print("Java class name: %s" % rsmd.getColumnClassName(col_no))
print("column type name: %s" % rsmd.getColumnTypeName(col_no))
print('-' * 20)
Output for interesting columns:
value: [03.01.11]
name: [date_column]
Java type: 91
Java class name: java.sql.Date
column type name: date
--------------------
value: [2011-01-03 00:00:00.0]
name: [datetime_yd]
Java type: 93
Java class name: java.sql.Timestamp
column type name: datetime year to day
--------------------
value: [2011-01-03 10:28:51.0]
name: [datetime_ys]
Java type: 93
Java class name: java.sql.Timestamp
column type name: datetime year to second
--------------------
value: [10:28:51]
name: [datetime_hs]
Java type: 92
Java class name: java.sql.Time
column type name: datetime hour to second
So correct answer for this version of JDBC driver is: datetime year to second
is mapped to java.sql.Timestamp
.
PS You wrote thay your driver maps this to java.sql.Time
but maybe you use some old version of Informix JDBC driver?
Have you tried using java.sql.Date ? I have no knowledge of Informix, however it seems to be the natural type for such a data, no ?
you can use java.util.Date, if you want to access hour, minute and second values..
yes i got your question .. You can use a Timestamp
data Type
ex....
Timestamp temp;
精彩评论