Solved

how to select DOB based on month (query problem)

Posted on 2003-12-01
10
474 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Find out what the Office 365 disclaimer function is, why you would use it and its limited ability to create Office 365 signatures.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

11 Experts available now in Live!

Get 1:1 Help Now