import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
Statement st = conn.createStatement();
st.executeUpdate("create table survey (id int,myDate DATE );");
String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";
PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
pstmt.setString(1, "1");
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
pstmt.setDate(2, sqlDate);
pstmt.executeUpdate();
ResultSet rs = st.executeQuery("SELECT * FROM survey");
Calendar cal = Calendar.getInstance();
// get the TimeZone for "America/Los_Angeles"
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
cal.setTimeZone(tz);
while (rs.next()) {
// the JDBC driver will use the time zone information in
// Calendar to calculate the date, with the result that
// the variable dateCreated contains a java.sql.Date object
// that is accurate for "America/Los_Angeles".
java.sql.Date dateCreated = rs.getDate(2, cal);
System.out.println(dateCreated);
}
rs.close();
st.close();
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
|