| Author |
Inserting formatted Date into DB from java
|
Disha Sharma
Greenhorn
Joined: Nov 15, 2011
Posts: 3
|
|
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
Joined: Jan 10, 2002
Posts: 56160
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Danny Baquilod
Greenhorn
Joined: Jun 26, 2011
Posts: 13
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Use DateFormat / SimpleDateFormat and their parse methods.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Danny Baquilod
Greenhorn
Joined: Jun 26, 2011
Posts: 13
|
|
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
Joined: Jan 10, 2002
Posts: 56160
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
Danny Baquilod wrote:
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
Joined: Jun 26, 2011
Posts: 13
|
|
Rob Spoor wrote:
Danny Baquilod wrote:
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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
Joined: Jun 26, 2011
Posts: 13
|
|
Thanks Rob, now it works
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
You're welcome
|
 |
 |
|
|
subject: Inserting formatted Date into DB from java
|
|
|