Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Date's related issues.

Posted on 2003-11-27
6
Medium Priority
?
256 Views
Last Modified: 2010-04-01
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;
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   conn = DriverManager.getConnection("jdbc:odbc:myDSN");
  PreparedStatement ps = conn.prepareStatement( "SELECT * FROM PURCHASE WHERE Date = ?");
  rs= ps.executeQuery();

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


errorr... lot of error...
0
Comment
Question by:acl6804
  • 4
6 Comments
 
LVL 1

Accepted Solution

by:
yapsing earned 1000 total points
ID: 9835805
i think your sql should be:
PreparedStatement ps = conn.prepareStatement( "SELECT * FROM PURCHASE WHERE Date = "+mth);

0
 
LVL 1

Expert Comment

by:yapsing
ID: 9835819
r u using Oracle, Access or MySQL???
0
 
LVL 1

Expert Comment

by:yapsing
ID: 9835895
to select data using the month in MySQL
PreparedStatement ps = conn.prepareStatement( "SELECT * FROM PURCHASE WHERE month(date) ="+mth);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:kotakinabalu
ID: 9835939
yapsing, i am using access.
but event i couldnt getthe month from the incoming date entry...
0
 
LVL 6

Expert Comment

by:jarasa
ID: 9836365
HI
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:
ps.setString(1,month);
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");
if(month==null)
    month=(String) request.getAttirbute("month");
if(month==null){
    System.out.println("Error moth is NULL");
    return the error to the client.
}else{
keep working
}

If the date is well fixed you should make this sentence before the ExecuteQuery
ps.setDate(1,mth);

Hope it helps
Javier


0
 
LVL 1

Expert Comment

by:yapsing
ID: 9840780
i think he accepted the wrong answer. mine can't be the solution
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ranking ecommerce websites is a vital process. You need to have a strong SEO (Search Engine Optimization) strategy. If you don’t have one, you are losing out on brand impressions, clicks and sales. Check this guide on how to improve website traffic …
The decision to migrate to the cloud is not a simple one—many factors, such a cost, ease of use, and ongoing maintenance come into play. The goal is always for cloud platforms to make storage and backups more seamless and effective. Here’s a look at…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

824 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