Solved

how to select DOB based on month (query problem)

Posted on 2003-12-01
10
476 Views
Last Modified: 2010-04-01
hi, i have a problemhere. this is the data i have in my table.

Dob
19/4/1977
26/12/1927
23/3/1958
.
.
.


And this is my form,

input.jsp
======
<select name="dob">
   <option value="01"> Jan</option>
   <option value="02"> Feb</option>
   <option value="03">March </option>
   <option value="04"> April</option>
.
.
.
</select>

confirm.jsp
========
<%
String dob =request.getParameter("dob");
SimpleDateFormat df = new SimpelDateFormat("MM");

  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   conn = DriverManager.getConnection("jdbc:odbc:myDSN");
PreparedStatement  ps = conn.prepareStatement( "SELECT * FROM Customer where dob= ? "
    ps.setDate( 1, new java.sql.Date( df.parse(startdate).getTime()));


** My problem is i don't know how to minimun the result when it's only match the middle character of the month... equal with the user's input...

User can only enter the month of the year, whereby the date stored in is in dd/MM/yyyy. So, what is the exact or correct SQL statment that i should use.. please guide me.
 
0
Comment
Question by:ipoh1977
  • 7
  • 3
10 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 200 total points
ID: 9848823
Can't you do:

PreparedStatement  ps = conn.prepareStatement( "SELECT * FROM Customer where month( dob ) = ?" ) ;
ps.setInt( 1, Integer.parseInt( dob ) ) ;
0
 

Author Comment

by:ipoh1977
ID: 9849281
TimYates, you mean this is the correct way?

<%
String dob =request.getParameter("dob");
SimpleDateFormat df = new SimpelDateFormat("MM");

 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  conn = DriverManager.getConnection("jdbc:odbc:myDSN");
PreparedStatement  ps = conn.prepareStatement( "SELECT * FROM Customer where month( dob ) = ?" ) ;
ps.setInt( 1, Integer.parseInt( dob ) ) ;
%>

how does it work? It's prompt me error when i complie the code. Saying
customer_0005fdo_0005fresult$jsp.java [83:1] cannot resolve symbol
symbol  : variable dob
location: class org.apache.jsp.customer_0005fdo_0005fresult$jsp
                    ps.setInt( 1, Integer.parseInt( dob ) ) ;
                                                    ^
1 error

The "dob" is my table field and the "month" is the user entry. and this is my original code:

<%
String dob =request.getParameter("dob");
SimpleDateFormat df = new SimpelDateFormat("MM");

 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  conn = DriverManager.getConnection("jdbc:odbc:myDSN");
PreparedStatement  ps = conn.prepareStatement( "SELECT * FROM Customer where dob= ? "
   ps.setDate( 1, new java.sql.Date( df.parse(startdate).getTime()));

Can you show the complete code for me?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9849313
<%
  String dob = request.getParameter("dob");
  if( dob != null )
  {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:myDSN");
    PreparedStatement  ps = conn.prepareStatement( "SELECT * FROM Customer WHERE MONTH( dob ) = ?" ) ;
    ps.setInt( 1, Integer.parseInt( dob ) ) ;
  }
%>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9849315
Should do it...

It uses the SQL "MONTH" function in the WHERE clause, so that it should match all rows where the month entered (dob) equals the month of the dob field...

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9849330
ASSUMING!!

the "dob" field in your database is a Date field or a Timestamp field...

I hope it is...

Otherwise, you will need something like:

PreparedStatement  ps = conn.prepareStatement( "SELECT * FROM Customer WHERE dob LIKE ?" ) ;
ps.setString( 1, "%/" + dob + "/%" ) ;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ipoh1977
ID: 9854682
Tim, thanks for your reply. NullPointer Exception. Surprise...
this is the code i changed.

RawData
=======
Dob
11/27/2003
11/15/2003
11/12/2003
11/28/2003
11/17/1999
11/5/2003
11/28/2003
11/27/2003
11/12/2003
11/20/2003
11/26/2003
11/19/2003
11/6/2003
11/20/2003
11/5/2003
11/12/2003
12/16/2003

input page
=======
.
    <tr><td>Month : </td>
<td>
    <select name="month">
        <option value="01">O1</option>
        <option value="11">11</option>
    </select>
</td>
.

result page
========
String dob=request.getParameter("month");
try {
Connection conn=null;
ResultSet rs=null;
if (dob!=null) {
    System.out.println(dob);
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   conn = DriverManager.getConnection("jdbc:odbc:myDSN");
    PreparedStatement  ps = conn.prepareStatement( "SELECT * FROM Customer where MONTH(Dob) = ?" ) ;
    ps.setInt( 1, Integer.parseInt( dob ) ) ;

while (rs.next()) {
    out.println(rs.getString("Customer"));
    out.println("<BR>");
}

} else { out.println("Null Value"); }


} catch (Exception sqle) {
                       sqle.printStackTrace();
    }

********

But this is the error i get. And i still can see the month is contain value. The "Dob" is a date type.

11
java.lang.NullPointerException
        at org.apache.jsp.customer_0005fdo_0005fresult$jsp._jspService(customer_0005fdo_0005fresult$jsp.java:87)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet$JspServletWrapper.service(IDEJspServlet.java:173)
        at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet.serviceJspFile(IDEJspServlet.java:246)
        at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet.service(IDEJspServlet.java:339)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:226)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9856642
You never execute the statement, and get the resultset back!!

....
ps.setInt( 1, Integer.parseInt( dob ) ) ;

// NEED THIS
rs = ps.executeQuery() ;

while (rs.next()) {
....

0
 
LVL 35

Expert Comment

by:TimYates
ID: 9856650
Don't forget to close your connection, and statement too:

String dob=request.getParameter("month");
Connection conn=null;
PreparedStatement  ps = null ;
ResultSet rs=null;
try
{
    if (dob!=null)
    {
        System.out.println(dob);
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn = DriverManager.getConnection("jdbc:odbc:myDSN");
        ps = conn.prepareStatement( "SELECT * FROM Customer where MONTH(Dob) = ?" ) ;
        ps.setInt( 1, Integer.parseInt( dob ) ) ;

        rs = ps.executeQuery() ;

        while (rs.next())
        {
            out.println(rs.getString("Customer"));
            out.println("<BR>");
        }
    }
    else
    {
        out.println("Null Value");
    }
}
catch (Exception sqle)
{
    sqle.printStackTrace();
}
finally
{
    try { if( rs != null ) rs.close() ; } catch( Exception ex ) { }
    try { if( ps != null ) ps.close() ; } catch( Exception ex ) { }
    try { if( conn != null ) conn.close() ; } catch( Exception ex ) { }
}
0
 

Author Comment

by:ipoh1977
ID: 9857057
oh... yeah.. i overlook already... so sorry.. it's my fault.

TQ for your help TimYates.

0
 
LVL 35

Expert Comment

by:TimYates
ID: 9857067
No worries :-)

Hee hee, I make mistakes like that all the time ;-)

I find a walk in the fresh air with a cup of coffee does wonders for finding easy mistakes ;-)

Good luck with it!!

Tim
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 37
constructor overloading 2 79
Clear browser cache on site login, is it possible? 3 24
How to Make JSP Pages Responsive ? details are below 3 594
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

25 Experts available now in Live!

Get 1:1 Help Now