Solved

how to select DOB based on month (query problem)

Posted on 2003-12-01
10
480 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 48
bean mapper frameworks 5 63
constructor overloading 2 86
squirrelPlay java challenge 40 158
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audi…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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