Tulsi Hemrajani
Tulsi Hemrajani

Reputation: 1

how to insert current date in mysql database using jsp?

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.

Upvotes: 0

Views: 8875

Answers (4)

tmwanik
tmwanik

Reputation: 1661

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

Upvotes: 5

Shailesh Saxena
Shailesh Saxena

Reputation: 3502

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();

Upvotes: 0

Kiran Jujare
Kiran Jujare

Reputation: 178

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.

Upvotes: 1

Rakhitha
Rakhitha

Reputation: 328

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

Upvotes: 0

Related Questions