Link to home
Start Free TrialLog in
Avatar of star5
star5

asked on

need simple modification to sql command!!

i have this code to display employees hired between two different days .
what is wrong with the sql command , it does no display anithing.??

date1 and date2 where sent from html form.
==================================
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class HireDate extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse res)
                                     throws ServletException, IOException {
 
         Connection con = null;          
         boolean logged = false;
         res.setContentType("text/html");
         PrintWriter out=res.getWriter();
         out.println("<HTML>");
         out.println("<HEAD><TITLE>Employee Hire Date</TITLE></HEAD>");
         out.println("<BODY BGCOLOR=\"#FFFFF\">");
         out.println("<CENTER>");
         out.println("<BR><BR>");
       try {
       
            String strSQL,strSQL1, Date1=null,Date2=null;
            ResultSet rs=null, rs1=null;
            Statement stmt;
           
            // get parameters passed from html
            Date1 = req.getParameter("date1");
         Date2 = req.getParameter("date2");      
            Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:db1");
            stmt = con.createStatement();
               
            strSQL = "select * from Training" +
                 " where startdate between" + "{d 'date1'} and" + " {d 'date2'} " + " Order by Startdate ";
            rs = stmt.executeQuery(strSQL);
               
               
               
            out.println("<TABLE CELLSPACING=\"0\" CELLPADDING=\"6\" BORDER=\"1\" BGCOLOR=#C0FFFF>");                                                                              
            out.println("<TR><TD>EmpNO.</TD><TD>Course Name</TD><TD>Start Date</TD><TD>End Date</TD><TD>Place</TD></TR>");      
           
                while(rs.next())                                                      
         {                                                                      
            out.println("<TR>");                                                              
            out.println("<TD BGCOLOR=#FFDAB9>" + rs.getString("EmpNO") + "</TD>");              
            out.println("<TD BGCOLOR=#FFC0CB>" + rs.getString("Coursename") + "</TD>");
             out.println("<TD BGCOLOR=#D8BFD8>" + rs.getString("Startdate") + "</TD>");
            out.println("<TD BGCOLOR=#EEE8AA>" + rs.getString("Enddate") + "</TD>");
            out.println("<TD BGCOLOR=#FFE4C4>" + rs.getString("Place") + "</TD>");
            out.println("</TR>");                                              
        }                                    
              rs.close();
              stmt.close();

       } catch (Exception e)
         {
           e.printStackTrace();
       }
         finally
         {
            if (con != null)
               {
                 try
                     {
                      con.close();
                 } catch (SQLException se) {
                      se.printStackTrace(System.err);
                 }
            }
       }
       out.println("</CENTER>");                                                
       out.println("</BODY>");                                                  
       out.println("</HTML>");  
  }    

}
ASKER CERTIFIED SOLUTION
Avatar of msterjev
msterjev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of star5
star5

ASKER

what u r saying is not clear !!
suppose this the code:
strSQL = "select * from Training" +
                " where startdate between" + "{d 'date1'} and" + " {d 'date2'} " + " Order by Startdate ";

how to modify it ?
note : remember those days are not constant , it changes according to what the user enter on the text box...
String date1="08/28/73";
String date2="03/21/01";

strSQL = "select * from Training" +
               " where startdate between #"+date1+"# and #" + date2+"# Order by Startdate";
Where are the points. Your's questions speed is in order of magnitude higher of your's accepting answers will! :-))))
Avatar of star5

ASKER

not working !!
check a gain
Depending on the database and your regional settings, the format of the date strings may be of importance. To get around this, i'd use a PreparedStatement instead.
Like this

//-->
try {
Date dDate1 = java.util.DateFormat.getDateInstance(DateFormat.SHORT).parse(Date1);
Date dDate2 = java.util.DateFormat.getDateInstance(DateFormat.SHORT).parse(Date2);
} catch (ParseException pe) {
// could not parse date-string(s)
// please handle me!
}
PreparedStatement pStmt = con.prepareStatement("select * from training where startdate between ? and ?");
pStmt.setDate(1, dDate1);
pStmt.setDate(2, dDate2);
RecordSet rs = pStmt.executeQuery();
//<--

Now, the DateFormat.parse() function may throw exceptions depending on the format of your date-strings. If so, you can either try to specify DateFormat.MEDIUM or DateFormat.LONG instead of DateFormat.SHORT in the getDateInstance() function. You can also try to use the java.util.SimpleDateFormat class instead, which allows you to specify the exact format of the input. Try it out and tell me how it went.

Regards PAP
Well, the above won't compile of course. Do like this
//-->
Date dDate1 = null;
Date dDate2 = null;
try {
dDate1 = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(Date1);
dDate2 = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(Date2);
} catch (java.text.ParseException pe) {
// could not parse date-string(s)
// please handle me!
System.out.println("Exception parsing date strings:" + pe);
}
PreparedStatement pStmt = con.prepareStatement("select * from training where startdate between ? and
?");
pStmt.setDate(1, dDate1);
pStmt.setDate(2, dDate2);
RecordSet rs = pStmt.executeQuery();
//<--
Well, the above won't compile of course. Do like this
//-->
Date dDate1 = null;
Date dDate2 = null;
try {
dDate1 = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(Date1);
dDate2 = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(Date2);
} catch (java.text.ParseException pe) {
// could not parse date-string(s)
// please handle me!
System.out.println("Exception parsing date strings:" + pe);
}
PreparedStatement pStmt = con.prepareStatement("select * from training where startdate between ? and
?");
pStmt.setDate(1, dDate1);
pStmt.setDate(2, dDate2);
RecordSet rs = pStmt.executeQuery();
//<--
Well, the above won't compile of course. Do like this
//-->
Date dDate1 = null;
Date dDate2 = null;
try {
dDate1 = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(Date1);
dDate2 = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(Date2);
} catch (java.text.ParseException pe) {
// could not parse date-string(s)
// please handle me!
System.out.println("Exception parsing date strings:" + pe);
}
PreparedStatement pStmt = con.prepareStatement("select * from training where startdate between ? and
?");
pStmt.setDate(1, dDate1);
pStmt.setDate(2, dDate2);
RecordSet rs = pStmt.executeQuery();
//<--
Well, the above won't compile of course. Do like this
//-->
Date dDate1 = null;
Date dDate2 = null;
try {
dDate1 = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(Date1);
dDate2 = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(Date2);
} catch (java.text.ParseException pe) {
// could not parse date-string(s)
// please handle me!
System.out.println("Exception parsing date strings:" + pe);
}
PreparedStatement pStmt = con.prepareStatement("select * from training where startdate between ? and
?");
pStmt.setDate(1, dDate1);
pStmt.setDate(2, dDate2);
RecordSet rs = pStmt.executeQuery();
//<--
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20284495.html
https://www.experts-exchange.com/questions/Q.20284978.html
https://www.experts-exchange.com/questions/Q.20286874.html
https://www.experts-exchange.com/questions/Q.20288082.html
https://www.experts-exchange.com/questions/Q.20287488.html
https://www.experts-exchange.com/questions/Q.20288869.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20285624.html
https://www.experts-exchange.com/questions/Q.20286675.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> https://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
Thanks for returning and finalizing this BUT two questions.
Why zero points and why a "C" grade?  Easy questions begin at a value of 50 points, the maximum per question is 300 points.  

Most experts here would prefer not to receive points than to receive a "C" grade, since we take great pride in collaborating and working to achieve "A" level results.  I am at a loss to understand why you post a zero point question here.  Please advise.  If this truly was not helpful to you, we'll delete it.

Expert input welcome as well.

Moondancer - EE Moderator