I am reading few data from a text file using java code,along with date in format (30-OCT-2012 12-22-44-991) and i want to store these details in Oracle Database but in the same format as used by oracle. I tried To_date but of no use, it gives error. Kindly help me.
问问题
7812 次
2 回答
1
在 Java 中使用SimpleDateFormat将您的 String 解析为java.util.Date
. 然后使用 aPreparedStatement
并在其上设置日期。
于 2013-01-20T10:44:02.917 回答
1
编辑:
您可以使用PreparedStatement
Aleksander Blomskøld 已经建议的方法,并在Using Oracle to_date function for date string with milliseconds的帮助下:
final sql = "INSERT into IFT_VEHICLE_STATUS (LATITUDE, LONGITUDE, UPDATED_AT) " +
"VALUES (?, ?, to_timestamp(?, 'DD.MM.YYYY HH24:MI:SS:SSFF3'))";
final PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, 81000000);
pstmt.setInt(2, 162000000);
pstmt.setDate(3, oracleDate);
pstmt.execute();
老:
你想把 a 转换java.util.Date
成 ajava.sql.Timestamp
吗?你可以这样做:
try {
final Date javaUtilDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-01-20 11:46:06");
System.out.println("javaUtilDate = " + javaUtilDate);
final Timestamp oracleDate = new Timestamp(javaUtilDate.getTime());
System.out.println("oracleDate = " + oracleDate);
} catch (ParseException e) {
e.printStackTrace();
}
这将给出以下输出:
javaUtilDate = Fri Jan 20 11:46:06 CET 2012
oracleDate = 2012-01-20 11:46:06.0
于 2013-01-20T10:53:12.337 回答