Solved

Date's related issues.

Posted on 2003-11-27
6
246 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xjc and jaxb 2 93
library class in java 1 92
Struts tiles for layout structure along with Angular JS. 4 163
jsp login check 12 32
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This problem is more common than not and I will show you some things to check to solve this problem.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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