import java.io.*;
import java.sql.*;
import java.text.*;
public class TestDateTypes {
Connection conn;
public TestDateTypes() {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
}
catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args)
throws Exception, IOException {
new TestDateTypes().process();
}
public void process() throws IOException, SQLException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = new Date(System.currentTimeMillis());
Time time = new Time(System.currentTimeMillis());
Timestamp stmp = new Timestamp(System.currentTimeMillis());
Statement stmt = null;
int rslt = 0;
try {
stmt = conn.createStatement();
rslt = stmt.executeUpdate(
"insert into test_data_types ( a_date ) values ( to_date( '" +
sdf.format(date) + "', 'YYYYMMDDHH24MISS' ) )");
rslt = stmt.executeUpdate(
"insert into test_data_types ( a_date ) values ( to_date( '" +
sdf.format(time) + "', 'YYYYMMDDHH24MISS' ) )");
rslt = stmt.executeUpdate(
"insert into test_data_types ( a_date ) values ( to_date( '" +
sdf.format(stmp) + "', 'YYYYMMDDHH24MISS' ) )");
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (stmt != null) try { stmt.close(); } catch (SQLException ignore) { }
}
}
protected void finalize()
throws Throwable {
if (conn != null) { try { conn.close(); } catch (SQLException ignore) { } }
super.finalize();
}
} |