Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to select DOB based on month (query problem)

Posted on 2003-12-01
10
Medium Priority
?
487 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
Tech spooks aren't just for those who are tech savvy, it also happens to those of us running a business. Check out the top tech spooks for business owners.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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