• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting formatted Date into DB from java

 
Disha Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we insert a formatted Date object into a table.
Table's date column is of data type : Date
format required is : MM/DD/YYYY HH:MM:ss a
I have formatted current time and getting the output properly in string.
Now how can I have it stored in Date object.
parse method of SimpleDateFormat converts it into again same without format.

Please suggest.

TIA.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java begineer wrote:I have formatted current time and getting the output properly in string.

Incorrect strategy.

Dates are just dates and have no format. They only have a format when they are converted into a string -- which there is absolutely no need to do when inserting into a Date column. Just leave it as a Date.

My guess is that you are not properly using PreparedStatement, and that you are using Statement and trying to format a SQL statement by converting the Date into a string. If so, wrong, wrong, wrong.

Use a PreparedStatement, and add the date value as a Date, not a String, using the setDate() method.
 
Danny Baquilod
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since we have the same issue, i'd rather use this topic to post my question.

//Servlet

String entryDate = request.getParameter("dEntryDate");

//How should I convert the above entryDate String to date? I need it to save to database table using setDate().


pst.setDate(34, dEntryDate);

Thanks in advance for your help.

Daniel
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use DateFormat / SimpleDateFormat and their parse methods.
 
Danny Baquilod
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Use DateFormat / SimpleDateFormat and their parse methods.



Thanks. Would you mind putting the script here?

I should start by getting the string from JSP...

String entryDate = request.getParameter("dEntryDate");

I tried doing that method from other forum but still getting error. THanks.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Baquilod wrote:I tried doing that method from other forum but still getting error.

Don't keep the error a secret.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Baquilod wrote:
Rob Spoor wrote:Use DateFormat / SimpleDateFormat and their parse methods.



Thanks. Would you mind putting the script here?

Please SearchFirst. There are millions of examples on the Internet, including hundreds (if not thousands) on the Ranch itself.
 
Danny Baquilod
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Danny Baquilod wrote:
Rob Spoor wrote:Use DateFormat / SimpleDateFormat and their parse methods.



Thanks. Would you mind putting the script here?

Please SearchFirst. There are millions of examples on the Internet, including hundreds (if not thousands) on the Ranch itself.


Here's the script :







1. try {
2. String dateCreate = request.getParameter("dateCreate");
3.
4. SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
5.
6. Date dateCreated = formater.parse(dateCreate);
7. newDate = dateCreated;
8. } catch (ParseException e )
9. {
10. e.printStackTrace();
11. }
12.
13. pst.setDate(6, newDate);
at line 13, it shows an error below.
// cannot find symbol
// symbol: method setDate(int, java.util.Date)
// location: interface java.sql.PreparedStatement

thanks in advance.

 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you got the parsing correct. Now all you need to do is turn a java.util.Date object into a java.sql.Date object:
Likewise for java.sql.Time and java.sql.Timestamp.
 
Danny Baquilod
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Well, you got the parsing correct. Now all you need to do is turn a java.util.Date object into a java.sql.Date object:
Likewise for java.sql.Time and java.sql.Timestamp.


Thanks Rob, now it works
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic