?
Solved

how to select DOB based on month (query problem)

Posted on 2003-12-01
10
Medium Priority
?
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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.

Question has a verified solution.

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

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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