File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Inserting formatted Date into DB from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting formatted Date into DB from java" Watch "Inserting formatted Date into DB from java" New topic
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: 61095
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Danny Baquilod
Greenhorn

Joined: Jun 26, 2011
Posts: 14
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: 19680
    
  18

Use DateFormat / SimpleDateFormat and their parse methods.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Danny Baquilod
Greenhorn

Joined: Jun 26, 2011
Posts: 14
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

Joined: Jan 10, 2002
Posts: 61095
    
  66

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: 19680
    
  18

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

Joined: Jun 26, 2011
Posts: 14
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

Joined: Oct 27, 2005
Posts: 19680
    
  18

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: 14
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

Joined: Oct 27, 2005
Posts: 19680
    
  18

You're welcome
 
Consider Paul's rocket mass heater.
 
subject: Inserting formatted Date into DB from java