简体   繁体   English

如何使用Java在Oracle中插入自定义日期时间?

[英]How to insert custom date time in oracle using java?

Hi i have a column (type date).I want to insert custom date and time without using Preparedstatement .i have used 嗨,我有一列(日期类型)。我想插入自定义日期和时间而不使用Preparedstatement

String date = sf.format(Calendar.getInstance().getTime());
String query = "Insert into entryTbl(name, joinedDate, ..etc)  values ("abc", to_date(date, 'yyyy/mm/dd HH:mm:ss'))";
statement.executeUpdate(query);

but am getting literal doesnot match error. 但是获取文字不匹配错误。 so even tried with "SYSDATE" .Its inserting only date not time.So how to insert the datetime using java into oracle?please any one help.. 所以甚至尝试使用“ SYSDATE”。它只插入日期而不是时间。那么如何使用java将oracle插入datetime?请提供任何帮助。

It is strongly recommended to use PreparedStatement to counter potential SQL injection attacks, instead of building raw SQL queries to interact with the database. 强烈建议使用PreparedStatement来应对潜在的SQL注入攻击,而不是构建原始SQL查询以与数据库进行交互。

That said, you can specify the value for date column as a string in yyyy-MM-dd HH:mm:ss format as well: 也就是说,您也可以使用yyyy-MM-dd HH:mm:ss格式将date列的值指定为字符串:

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sf.format(Calendar.getInstance().getTime());
String query = "INSERT INTO entryTbl(name, joinedDate, ..etc.,)  values ('abc', '" 
    + date + "', ...etc.,)";
statement.executeUpdate(query);

I think yyyy/mm/dd HH:mm:ss should be updated as : YYYY/MM/DD HH:MI:SS TZH:TZM and use to_timestamp -- documentation here 我认为yyyy/mm/dd HH:mm:ss应该更新为: YYYY/MM/DD HH:MI:SS TZH:TZM并使用to_timestamp 这里的文档

 String query = "INSERT INTO entryTbl(name, joinedDate, ..etc.,) "+
           "values ('abc', "+
           "TO_TIMESTAMP('"+dateString+"', 'YYYY/MM/DD HH:MI:SS TZH:TZM'), etc.,)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM