Here's the corrected memo. And I compiled it.
The JDBC escape format for Timestamp and Date is '1999-12-31 23: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:
import java.sql.*;
import java.util.GregorianCalendar;
class Ttest {
public static void main(String[] args) {
// 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
formilli's.
Timestamp tsvalue1 = 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).
Timestamp tsvalue2 = new Timestamp(99,11,31,23,59,59,999);
// Causes deprecation note.
try {
Connection con = DriverManager.getConnection("url");
PreparedStatement pstmt = con.prepareStatement("INSERT INTO TABLE_1
VALUES (?)");
pstmt.setTimestamp(1,tsvalue1);
pstmt.execute();
} catch (Exception e) {}
}
}
HTH,
Rich Katz
(Please toss the previous one in the bit bucket).
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.
|