How to Insert Current date into oracle database from a java program?
38,950
Solution 1
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:
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.
Solution 2
Just in case you need CURRENT date, use:
INSERT INTO YOUR_TABLE (YOUR_DATE_COLUMN, COLUMN_2, COLUMN_3)
VALUES (SYSDATE, 1234, 567);
Author by
Aswin
Updated on February 13, 2020Comments
-
Aswin about 4 years
I want to insert the current date to my oracle database Bills. I am getting the date from system using
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? -
A.B.Cade over 10 yearsMore than that, Oracle DB DATE datatype doesn't even have a format.. Sending a String will cause the DB to implicitly convert the String according to NLS_DATE_FORMAT which might be different in some environments
-
ArtOfWarfare over 7 yearsWhat's the difference between
SYSDATE
andCURRENT_DATE
as the person who commented on the question suggested? Edit: Found the answer.SYSDATE
is the date on the server whileCURRENT_DATE
is the date on the client. SoSYSDATE
is probably almost always the desired one, I'd think. -
Shashank almost 6 yearsIf there is a session timezone set, does it write the date in that timezone or always UTC?