如何从java程序将当前日期插入oracle数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19618975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Insert Current date into oracle database from a java program?
提问by Aswin
I want to insert the current date to my oracle database Bills. I am getting the date from system using
我想将当前日期插入到我的 oracle 数据库 Bills 中。我正在使用系统从系统获取日期
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
is that possible to directly insert it into the database without converting to the default format (dd-month-yyyy
) of DATE data type in Oracle?
是否可以直接将其插入数据库而不转换为dd-month-yyyy
Oracle 中 DATE 数据类型的默认格式 ( )?
采纳答案by Durandal
You did not give any context on what interface you are using to communicate with the DB. Assuming plain JDBC, let the driver handle the problem, use a statement object and set the parameter(s) properly:
您没有提供关于您使用什么接口与数据库通信的任何上下文。假设普通 JDBC,让驱动程序处理问题,使用语句对象并正确设置参数:
Connection connection = ...
PreparedStatement statement = connection.prepareStatement("INSERT INTO <TableNameHere> VALUES (?)");
statement.setObject(1, new java.sql.Date());
statement.executeUpdate();
(Error handling and fluff ommited) The JDBC driver will deal with the details on how to format the date as the database wants it. Don't even worry about it.
(省略错误处理和绒毛)JDBC 驱动程序将处理有关如何根据数据库的需要格式化日期的详细信息。甚至不用担心。
回答by ajc
1) It is not mandatory to use the default dd/MM/yyyy format in while initializing SimpleDateFormatter. You have a list of options which can match to your requirement. SimpleDateFormat
1) 初始化 SimpleDateFormatter 时不强制使用默认的 dd/MM/yyyy 格式。您有一个可以满足您要求的选项列表。简单日期格式
回答by yurin
Just in case you need CURRENT date, use:
以防万一您需要当前日期,请使用:
INSERT INTO YOUR_TABLE (YOUR_DATE_COLUMN, COLUMN_2, COLUMN_3)
VALUES (SYSDATE, 1234, 567);