Date's related issues.

Dear all,
i have this code. What i intended to do is get the user's date, dd/MM/yyyy and based on this user's entried date, i wish to retrieve all the data in the same month with it...
this is my code... but doesn't work with the date. Don't know how to convert here and there..... please correct me.

String month=request.getParameter("month");
SimpleDateFormat sf = new SimpleDateFormat("MM");
String mth= sf.format(month);

Connection conn=null;
ResultSet rs=null;
   conn = DriverManager.getConnection("jdbc:odbc:myDSN");
  PreparedStatement ps = conn.prepareStatement( "SELECT * FROM PURCHASE WHERE Date = ?");
  rs= ps.executeQuery();

while ( {
out.println("Date" + rs.getString("Date"));

errorr... lot of error...
Who is Participating?
i think your sql should be:
PreparedStatement ps = conn.prepareStatement( "SELECT * FROM PURCHASE WHERE Date = "+mth);

r u using Oracle, Access or MySQL???
to select data using the month in MySQL
PreparedStatement ps = conn.prepareStatement( "SELECT * FROM PURCHASE WHERE month(date) ="+mth);
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

yapsing, i am using access.
but event i couldnt getthe month from the incoming date entry...
The data field Date on your purchase table is a DATE or a VARCHAR?
if is a date you must transform if from the string, but also if you are receiving just the month not the full date you should do something to it either, if is a VARCHAR well then I guess you just have to put this sentence before the ExecuteQuery:
I also saw that this sentence
String month=request.getParameter("month");
should be
String month=(String) request.getParameter("month");
and you shouls check if it gets null becose maybe it cames as an attibute then you shold do this
String month=(String)request.getParameter("month");
    month=(String) request.getAttirbute("month");
    System.out.println("Error moth is NULL");
    return the error to the client.
keep working

If the date is well fixed you should make this sentence before the ExecuteQuery

Hope it helps

i think he accepted the wrong answer. mine can't be the solution
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.