0

I am looking for a way to get today's date and pass to sql table and save there. Call the saved date and do some task with JODA TIME API. The changed Joda time Date to sql table and save there and process continues..

I tried this way,

//prints todays date
java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());
//passes wrong date to the table like 1970-07-01 instead of 2013-03-01
String insert = "INSERT INTO TEST_TABLE VALUES(1,"+sqlDate+")";
pStmt = conn.prepareStatement(insert);
pStmt.executeUpdate();

//converting to joda time 
LocalDate ld = new LocalDate(sqlDate);

//some calculations, and how to convert back to sql date?

What I am trying to do here is, A table with 3 columns (id, startdate, finishdate). id will be entered by user, start date should be automatically entered todays date. after some calculations with joda time and finish date will be set to date it is finished.

Code

String insert = "INSERT INTO TEST_TABLE VALUES(2,'"+timestamp+"')";

Error

Data type mismatch in criteria expression
//I have created table using MS access
//the format of the date column is Date/Time.
vijay
  • 1,129
  • 7
  • 22
  • 34
  • have you seen this http://stackoverflow.com/questions/8992282/convert-localdate-to-localdatetime-or-java-sql-timestamp – Freak Mar 01 '13 at 10:41
  • could be please have a look at the updated question at bottom, I am facing the error, Thank you. – vijay Mar 01 '13 at 10:57
  • Please use prepared statements properly (by using a `?` in place of where you want your parameter to go, and then using the appropriate `setX()` function), your PS is still technically susceptible to SQL Injection. – Quetzalcoatl Mar 01 '13 at 11:34
  • I think you should accept the answer as It helped you out for your basic question – Freak Mar 04 '13 at 10:22

1 Answers1

4

You Can use Timestamp here. java.sql.Timestamp extends java.util.Date, so anything you can do with a java.util.Date you can also do with a java.sql.Timestamp.

To convert LocalDateTime to Timestamp

Timestamp timestamp = new Timestamp(localDateTime.toDateTime().getMillis());

But if You still want to convert Timestamp into java.sql.Date then use this

java.sql.Date date = new java.sql.Date(timeStamp.getTime());  
Freak
  • 6,786
  • 5
  • 36
  • 54
  • could be please have a look at the updated question at bottom, I am facing the error, Thank you. – vijay Mar 01 '13 at 10:56
  • 1
    i am not much familiar with MS access for storage purpose and its datatypes but try to insert timestamp into java.sql.Date and if you are still unable then convert the datatype of your ms access column to TEXT rather than date/time – Freak Mar 01 '13 at 11:00
  • *I mean try to convertt Timestamp into java.sql.Date – Freak Mar 01 '13 at 11:13
  • Thanks for your answer, I have converted the date column format to text, it was successful and able to read/write. To get the date from table I used rs.getDate(i) which i printed and success. But How can i convert the rs.getDate(i) into localdate again? – vijay Mar 01 '13 at 11:26
  • 1
    Keep in mind that ResultSet.getDate() returns a java.sql.Date, not a java.util.Date.So when you need to convert , you need to do this `LocalDate ld = new LocalDate(sqlDate);`. Please accept the answer, as it has solved your basic question.And don't use comments for further questions.Try to start a new thread. – Freak Mar 04 '13 at 04:00