Solved

need simple modification to sql command!!

Posted on 2002-04-06
12
235 Views
Last Modified: 2010-03-31
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>");  
  }    

}
0
Comment
Question by:star5
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 8 total points
ID: 6923170
Enclose the dates into the #, for example
BETWEEN #21.03.2001# AND .....
0
 

Author Comment

by:star5
ID: 6923455
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...
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6923510
String date1="08/28/73";
String date2="03/21/01";

strSQL = "select * from Training" +
               " where startdate between #"+date1+"# and #" + date2+"# Order by Startdate";
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6923586
Where are the points. Your's questions speed is in order of magnitude higher of your's accepting answers will! :-))))
0
 

Author Comment

by:star5
ID: 6927374
not working !!
check a gain
0
 
LVL 4

Expert Comment

by:pellep
ID: 6928059
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
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Expert Comment

by:pellep
ID: 6928069
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();
//<--
0
 
LVL 4

Expert Comment

by:pellep
ID: 6928071
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();
//<--
0
 
LVL 4

Expert Comment

by:pellep
ID: 6928073
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();
//<--
0
 
LVL 4

Expert Comment

by:pellep
ID: 6928097
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();
//<--
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6956461
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://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.
http://www.experts-exchange.com/questions/Q.20284495.html
http://www.experts-exchange.com/questions/Q.20284978.html
http://www.experts-exchange.com/questions/Q.20286874.html
http://www.experts-exchange.com/questions/Q.20288082.html
http://www.experts-exchange.com/questions/Q.20287488.html
http://www.experts-exchange.com/questions/Q.20288869.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20285624.html
http://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 http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed here -> http://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
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6956871
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
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now