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);
Share:
38,950
Aswin
Author by

Aswin

Updated on February 13, 2020

Comments

  • Aswin
    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
    A.B.Cade over 10 years
    More 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
    ArtOfWarfare over 7 years
    What's the difference between SYSDATE and CURRENT_DATE as the person who commented on the question suggested? Edit: Found the answer. SYSDATE is the date on the server while CURRENT_DATE is the date on the client. So SYSDATE is probably almost always the desired one, I'd think.
  • Shashank
    Shashank almost 6 years
    If there is a session timezone set, does it write the date in that timezone or always UTC?