0

I am trying to insert the date using this code:

java.sql.Timestamp sqlNow=new java.sql.Timestamp(new java.util.Date().getTime());
pstTimestamp(1,sqlNow);

On running the code, the result is successful, but the date is not been displayed in the database.

4
  • 1
    Provide more code and details Commented Mar 25, 2013 at 8:29
  • Where is JSP in coming into picture? Commented Mar 25, 2013 at 8:30
  • What is pstTimestamp(1,sqlNow); Is it your customized method? If yes then please provide the code as well. Commented Mar 25, 2013 at 8:31
  • basically i am making a registration page ... Commented Mar 25, 2013 at 8:40

4 Answers 4

5

You should use PreparedStatement and use it to set the date as follows :-

PreparedStatement pstmt = con.prepareStatement("INSERT INTO table_name (col_name) VALUES (?)");
pstmt.setTimestamp(1, new java.sql.Timestamp(new java.util.Date().getTime()));
pstmt.executeUpdate();

Read more here

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

PreparedStatement ps = con.prepareStatement("INSERT INTO table_name (col_name) VALUES (now())");
ps.executeUpdate();

use now() so that it will get the date in db format only.

Comments

0

Make sure you commit after running the command. Or make sure auto commit is enabled on the connection.

1 Comment

@ Rakhitha :by default auto commit is enabled on getting the connection until you are not setting it to false manually.
0

Just you need to provide the java.sql.Timestamp class object to the PreparedStatement object rest of work will be done by driver. I think there is a problem in your code while setting place holder's value(parameter). Use pstmnt.setTimestamp(int index, java.sql.Timestamp timestamp_object); now execute your query as: pstmnt.executeUpdate();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.