my dog learned polymorphism*
The moose likes Servlets and the fly likes Converting string to util.date & then sql.date to query a table 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 » Java » Servlets
Bookmark "Converting string to util.date & then sql.date to query a table" Watch "Converting string to util.date & then sql.date to query a table" New topic
Author

Converting string to util.date & then sql.date to query a table

Lijoy John
Ranch Hand

Joined: Jul 24, 2001
Posts: 32
Hi All,
I am trying to convert a String to a java.util.Date and then subsequently convert it into java.sql.Date to be queried in the database. Kindly let me know if there is a better approach for this .
In Servlets I have passed a method in which i am passing 2 parameters 'String from, String to':
and then based upon I want to get all the request(having 4 columns i.e req_id, name, status, create_date) in the database between those two dates. and then subsequently put in a table .
Note: In the database the date format is eg. 8/1/01 but when i click the cell(I am using a software to access the database) a dialog window comes up and shows the date as "Wednesday, August 01, 2001" . I am not sure if there is anything to do with this ?
This is the method allRequest():
public String allRequest(String from, String to) {
String sDateFormatter = "mm/dd/yy";
String fDate = from; // this is in the format 7/15/01
String toDate = to;
java.text.SimpleDateFormat formatter = new SimpleDateFormat(sDateFormatter);
try{
ffdate = formatter.parse(fDate);// ffdate is java.util.Date object
ttdate = formatter.parse(toDate);
System.out.println(ffdate); // this is just to see the out put on the console.
System.out.println(ttdate);
//-----------------------------------------------
System.out.println(fDate);// just printing the arguments in the method i.e 7/25/01
System.out.println(toDate); this is 8/2/01
long fSeconds = ffdate.getTime(); // as java.sql.Date object takes datatype long.
long tSeconds = ttdate.getTime();
fromdate = new java.sql.Date(fSeconds);
todate = new java.sql.Date(tSeconds);

System.out.println(fromdate); // here i am getting the output on console as '2001-01-25'
System.out.println(todate); // here i am getting the output on console as '2001-01-03'

}catch(ParseException ex){}
String query ="SELECT * FROM REQUEST WHERE CREATE_DATE BETWEEN ? and ? ";
StringBuffer sb = new StringBuffer("");
PreparedStatement pstate= null;
Connection con = null;
String nu = null;
System.out.println("**Getting The connection in ALLREQUEST**");
try{
con = getConnection();
if(con == null) {
System.out.println("Unable to establish DB connection!");
System.out.println("Cannot continue - Exiting");
return nu;
} else {
pstate = con.prepareStatement(query);
pstate.setDate(1, fromdate);// fromdate is java.sql. Date object
pstate.setDate(2, todate); // todate is java.sql. Date object
ResultSet rs = pstate.executeQuery();
sb.append("<TABLE>");// this is to add all the request in a table and show on browser.
sb.append("<TR>");// let me know if there is a better approach.
/*
* Its not reading rs.next() at all but just prints the last line sb.append("</table>");
* on the console
*/

while (rs.next()) {
int req_id = rs.getInt(1);
String domain = rs.getString(3);
int status = rs.getInt(4);
Date dt = rs.getDate(9);
String daate = dt.toString();// this is not working fine here.
sb.append("<TD>"+ req_id + "</TD><TD>" + domain + "</TD><TD>" + status + "</TD><TD>" +
daate + "</TD><TD>" + dt + "</TD>");
sb.append("</TR>");
}
sb.append("</TABLE>");
}
}
catch(SQLException ex) {
ex.getMessage();
System.out.println("*&EXCEPTION in allRequest&*");
ex.printStackTrace();
}
return sb.toString();
}
Kindly let me know how this problem could be solved.
Any help would be appreciated.
Thanks,
Regards,
John

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Please don't make duplicate posts in different forums. It wastes the time of people who don't realize that a question has already been answered. I'm closing this thread - see "Java in General - Advanced" for followups.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting string to util.date & then sql.date to query a table
 
Similar Threads
How to get all request stored in databse between two date
'A HREF = a row in Database' for each req_id in Servlets
Spring MVC AbstractWizardFormController: Cannot see ArrayList populated in controller
Converting String to util.Date and then sql.Date
ResultSet rs = prestatement.executeQuery()