Dear Irshad,
The JDBC escape format for Timestamp and Date is '1999-12-31 59:59:999'
You can use the escape format within a SQL statement, e.g: "INSERT INTO
TABLE_1 VALUES ('1999-12-31')"; But this may not be accepted exactly as
shown by all JDBC drivers.
The recommended way is to use a prepared statement and send it a
Timestamp: (I haven't compiled this - so just use it as an example).
// For Java 2: The Calendar classes take care of Timezone issues
// and year entry is 0 based.
// all Calendar values in raw form represent GMT.
GregorianCalendar cal = new GregorianCalendar();
cal.set(1999,11,31,59,59); // Parameterized set()
allows y,m,d,h,m,s but not milli's.
cal.set(cal.MILLISECOND,999); // So a 2nd set() is required for
milli's.
Timestamp tsvalue = new Timestamp(cal.getTime().getTime());
// Note: 1st getTime() converts Calendar to Date.
// 2nd getTime() converts Date to long.
// Timestamp constructor requires a long.
// For JDK 1.1.x (deprecated in JDK 1.2)
// the parameterized constructor does not account for Timezones
// and the year is 1900 based. (In both Timestamp and Calendar sets,
the month is 0 based).
Timestame tsvalue = new Timstamp(99,11,31,59,59,999);
PreparedStatement pstmt = con.prepareStatement("INSERT INTO TABLE_1
VALUES (?)");
pstmt.setTimestamp(1,tsvalue);
pstmt.execute();
HTH,
Rich Katz
Irshad Mungly wrote:
> Hi there,
>
> Can anybody tell me please how to insert a date fields in the database.
> e,g,
> insert into table_1 values (10/12/1999, ....
>
> This gives me an error.
>
> And also how to store time in a table.
> I must passed the information to a servlet.
>
> Thanks
>
> Irshad.
>
> To unsubscribe from this list, please an send
> email to 'majordomo@smartcard.co.uk' with the text
> 'unsubscribe instantdb' in the message body.
To unsubscribe from this list, please an send
email to 'majordomo@smartcard.co.uk' with the text
'unsubscribe instantdb' in the message body.
|