This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes sql exception when try to get date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sql exception when try to get date" Watch "sql exception when try to get date" New topic
Author

sql exception when try to get date

palmari sivasubramanian
Greenhorn

Joined: Mar 07, 2006
Posts: 6
Hi all,

I have created on html page to get Date of Birth.In jsp page,
String d=request.getParameter("day");
String m=request.getParameter("month");
String y=request.getParameter("year");
String dob=d+"/"+m+"/"+y;

In my table(app) i have given dob as one column and declared as DATE Type.

in JDBC,
i have used this statement
stmt.executeUpdate("insert into app values(TO_DATE('dob','dd/mm/yyyy'))");

it is not updating in my database. if i give sysdate, it is updating in the database.What is the problem in this? is the way of approch, correct to update the date in my database? help me out.

Thanks in advance
Palmari.
Mateus Lucio
Ranch Hand

Joined: Jul 27, 2006
Posts: 57
most Databases use english Date format
try this
stmt.executeUpdate("insert into app values(TO_DATE('dob','mm/dd/yyyy'))");

just a guess though


Studying ...
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Hi palmari sivasubramanian,

Welcome to Java Ranch

To insert date values into database, you should use java.sql.Date .

String dob = d+"/" + m+ "/" + y;
SimpleDateFormat sdf = new SImpleDateFormat("dd/MM/yyyy");
java.util.Date javaDate = sdf.parse(dob);
java.sql.Date sqlDate = new java.sql.Date(javaDate);


SimpleDateFormat (Java 2 Platform SE 5.0)
[ August 12, 2006: Message edited by: Saif Uddin ]

Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Saif Uddin:
String dob = d+"/" + m+ "/" + y;
SimpleDateFormat sdf = new SImpleDateFormat("dd/MM/yyyy");
java.util.Date javaDate = sdf.parse(dob);
java.sql.Date sqlDate = new java.sql.Date(javaDate);


We dont have this very, java.sql.Date(java.Util.Date date), constructor. Use new java.sql.Date(javaDate.getTime()).
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Mateus Lucio:

stmt.executeUpdate("insert into app values(TO_DATE('dob','mm/dd/yyyy'))");


Use PreparedStatement instead.
 
Consider Paul's rocket mass heater.
 
subject: sql exception when try to get date
 
Similar Threads
not able to get values into database
Problem with datetime fields
Struts Date Conversion
concatenate Text Fields in JSP
how to generate sql date