The drivers accept the
Date, Time, and Timestamp classes from the java.sql package and the
JDBC escape sequences for the same types.
The following code fragment
illustrates using the time-related classes in the java.sql package
with a PreparedStatement:
// stmt is a Statement object created with Connection.createStatement()
stmt.executeUpdate(
"create table sample.timeexamples (c1 date, c2 time, c3 timestamp)"
);
// Use the time-related classes in the java.sql package
// with a PreparedStatement
java.sql.Date d = new java.sql.Date(System.currentTimeMillis());
java.sql.Time t = new java.sql.Time(System.currentTimeMillis());
java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis());
PreparedStatement ps = c.prepareStatement("insert into sample.timeexamples
values (?, ?, ?)");
ps.setDate(1, d);
ps.setTime(2, t);
ps.setTimestamp(3, ts);
ps.executeUpdate();
// Let the classes in the java.sql package perform the
// JDBC escape sequences
ps.setDate(1, java.sql.Date.valueOf("2010-06-10"));
ps.setTime(2, java.sql.Time.valueOf("09:17:00"));
ps.setTimestamp(3, java.sql.Timestamp.valueOf("2010-06-10 09:17:00"));
ps.executeUpdate();
ps.close();
Alternatively, the JDBC
escape syntax can be coded manually to insert timestamps (keyword
ts
), dates (keyword
d
), and times (keyword
t
). The following
code fragment illustrates using the JDBC escape sequence with timestamp.
Timestamp ts = new Timestamp(System.currentTimeMillis());
// By using an escape clause with curly braces and the ts keyword,
// the following code is portable across the SAS Drivers for JDBC.
String myDate = new String ("{ts '" + ts.toString() + "'}");
String query =
"INSERT INTO work.testtable (index, ts, name) VALUES ("
+ i
+ ", "
+ myDate
+ ", "
+ n
+ ")";