• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic