Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP and SQL problem

 
Jeetendra S Ahuja
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sample Database
+-------+------+--------------+
| busno | fuel | date |
+-------+------+--------------+
| 1 | 19.6 | 16-09-2002 |
| 1 | 19.6 | 17-09-2002|
| 1 | 22.8 | 18-09-2002|
| 1 | 17.2 | 19-09-2002|
| 1 | 17.2 | 20-09-2002|
| 1 | 17.2 | 21-09-2002 |
| 1 | 17.2 | 22-09-2002|
| 2 | 17.2 | 23-09-2002 |
| 2 | 17.2 | 24-09-2002 |
| 2 | 17.2 | 25-09-2002 |
| 2 | 17.2 | 26-09-2002|
| 2 | 17.2 | 27-09-2002|
| 3 | 17.2 | 28-09-2002 |
| 3 | 19.7 | 29-09-2002 |
assume
A.jsp --> B.jsp

I have retrieved date in web page(say A.jsp) via util date object,now i had retrieved same date on other web page(B.jsp) via request.getParameter(),,,now i want to store that date in database,problem is that request.getParameter() gives string and in Database i have date DataType ,,so how to convert string date to sql date in JSP specially,, and after storing date :
I want to add fuel for a particular bus from UserDefined Date to UserDefined date....
I know this query:-select SUM(fuel) from fuel group by busno.
i dont know how to integrate between clause with it..... please help..i have to submit my project,,,asap....
Thanks Jeetendra Ahuja
 
Philip Thamaravelil
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me say first, doing this type of logic in a JSP violates many best practices.

What is sounds like your looking for is object serialization. Deflating an object (this case a java.util.Date), passing the string to another module, then re-inflating to the Object.

Now, implementing full-blown object serialization might be overkill for what you are wanting to accomplish, So IMO, I would use the java.text.SimpleDateFormat library to convert your date object to a string, then use the same library to get your java.util.Date object back.

BTW - java.util.Date can easily be converted to java.sql.Date. Have a look at the java.sql.Date constructors in it's API.

Best of luck.

Cheers,
Philip



 
Jeetendra S Ahuja
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s2=(request.getParameter("date"));
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date result = formater.parse(s2);

java.sql.Date date = new java.sql.Date(result);
Hey,,,philip after using this ,,even i am getting the error
Date(java.util.Date) constructor cannot find symbol..
 
Philip Thamaravelil
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's more of what I had in mind..




Let me suggest though, doing this type of task in JSP is inherently problematic and a poor practice. This should be done in an enterprise component such as a Servlet.

Cheers,
Philip
 
Philip Thamaravelil
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeetendra S Ahuja wrote:
Date(java.util.Date) constructor cannot find symbol..


You need to use the getTime() function.




Cheers,
Philip
 
Jeetendra S Ahuja
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey philip...,i did it under your guidance...
anyway,
CHEERS
 
Philip Thamaravelil
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's terrific! Glad I could help.
Philip
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic