Convert string to java.sql.Date to insert into databse

anshul9189
anshul9189 used Ask the Experts™
on
I am receiving 2 string values in a form which are in
yyyy-mm-dd format. i need to insert them in oracle database using PreparedStament.
i am doing the following:
  String txt1 = request.getParameter("txt1");
  String txt2 = request.getParameter("txt2");
       
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
      java.sql.Date effect_from = new java.sql.Date(formatter.parse(txt1).getTime());
      java.sql.Date end_on = new java.sql.Date(formatter.parse(txt2).getTime());
     
 But i am getting incorrect results.
plz help.    
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
> SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");

should be:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

mm is minutes

Author

Commented:
Thanks.
I have my string in yyyy-mm-dd format and i want to insert it in dd-mon-yyyy(which is the default format for oracle).
but it getting inserted as mm/dd/yyyy.
Mick BarryJava Developer
Top Expert 2010

Commented:
If you are using a PreparedStatement you don't need to worry about formatting, the driver looks after it for you.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
yes i am using preparedStatement.
But the values get inserted as mm/dd/yyyy

Author

Commented:
..and when i try to print values in the jsp page both the string value and  comverted value displays as same, that is yyyy-mm-dd. but when i see in the databse it is mm/dd/yyyy
Java Developer
Top Expert 2010
Commented:
> But the values get inserted as mm/dd/yyyy

not if its a datetime field. The database just stores the date, and not the formatting.

> but when i see in the databse it is mm/dd/yyyy

thats probably just the formatting used by whatever app ur using to view the db table contents.
You can use SimpleDateFormat to display it in whatever format you choose.

Author

Commented:
Thank you so much objects
Mick BarryJava Developer
Top Expert 2010

Commented:
hi anshul
i just use ur syntax ,but it gives error as below


C:\Tomcat 4.1\work\Standalone\localhost\test\user_insert_info1_jsp.java:86: cannot resolve symbol
symbol  : constructor Date (java.util.Date)
location: class java.sql.Date
  java.sql.Date indate = new java.sql.Date(sdf.parse(sdate));
                         ^
1 error
Mick BarryJava Developer
Top Expert 2010

Commented:
> java.sql.Date indate = new java.sql.Date(sdf.parse(sdate));

should be:

java.sql.Date indate = new java.sql.Date(sdf.parse(sdate).getTime());
hai objects
Thanks,now its working properly.
but i have one doubt,what is the necessity of getTime in that statement??
also one more doubt,how we can find the jdbc version we installed.

reagrds
chandrakanth.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial