Hi ,
I had been trying to figure out what could be the problem here.
I am having a
servlet which accepts two Dates i.e DateFrom and DateTo. I have to get all the request from the database between those dates. and the subsequent output to be shown in a browser in the form of a table . There are four columns which are
id NUMBER
name varchar
status NUMber
Create_date DATE
The following is the code :
public
String allRequest(String from, String to) {
/*
* from and to are coming from req.getParameter(from/to);
*/
String sDateFormatter = "mm/dd/yy";
String fDate = from;
String toDate = to;
java.text.SimpleDateFormat formatter = new SimpleDateFormat(sDateFormatter);
try{
ffdate = formatter.parse(fDate); // util.Date object
ttdate = formatter.parse(toDate); //util.Date object
long fSeconds = ffdate.getTime();
long tSeconds = ttdate.getTime();
fromdate = new java.sql.Date(fSeconds); // sql.Date object
todate = new java.sql.Date(tSeconds); // sql.Date object
System.out.println(fromdate);
System.out.println(todate);
}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;
try{
con = getConnection(); // this gets the coonection from pool
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);
pstate.setDate(2, todate);
ResultSet rs = pstate.executeQuery();
sb.append("<TABLE>");
sb.append("<TR>");
// Showing till here on the console and not taking rs.next()
while (rs.next()) {
System.out.println("**1**");
String requ_id = String.valueOf(rs.getInt(1));
String domaain = rs.getString(3);
String statuss = String.valueOf(rs.getInt(4));
Date dt = rs.getDate(9);
String daate = dt.toString();
sb.append("<TD>"+ requ_id + "</TD><TD>"
+domaain+ "</TD><TD>" + statuss + "</TD><TD>" + daate
+ "</TD>");
sb.append("</TR>");
} // while
sb.append("</TABLE>");
}
return sb.toString();
}
It would be kind to let me know how to proceed further or if there is a better approach to this. say for example I get 10 rows from the database, then all those rows( with 4 columns) should be shown on the browser.
Thanks,
Regards,
John