Solved

Date's related issues.

Posted on 2003-11-27
6
247 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 250 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
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.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
difference between member and local variables. 29 140
iterator example remove 8 75
doubleChar java challenge 7 121
JSONSerializer.toJSON in JSP generating error 8 81
The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
Riverbed Technology's webinar discusses networking for the cloud era with simplified SD-WAN cloud connectivity.
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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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