0

Why this query is not returning any value. There are values in my database with in that range

value input are

Date startDate = (Date) data.get("startDate");//2014-04-01 
Date endDate = (Date) data.get("endDate");//2014-04-30
int pagesize=10;

sql query is

String query = "select * from bill_details "
 + " where status=? "
 + " AND date(add_date) BETWEEN " + startDate + " AND " + endDate
 + " order by add_date desc limit " + pagesize;
ps = con.prepareStatement(query);
ps.setString(1, dataStatus);
System.out.println("Before execution");
res = ps.executeQuery();
Ravinder Reddy
24.1k6 gold badges54 silver badges86 bronze badges
asked Apr 19, 2014 at 7:55
7
  • What is this where status=?? Commented Apr 19, 2014 at 7:57
  • @HamletHakobyan Please see updated qs status='active' Commented Apr 19, 2014 at 7:59
  • 2
    You're leaving out single quotes around your date strings. At least without more info. Commented Apr 19, 2014 at 7:59
  • 1
    Use parameters instead of string concatenation. Commented Apr 19, 2014 at 8:01
  • 1
    BEWARE OF SQL INJECTION . Also BETWEEN is rarely the correct operator for dates - What do BETWEEN and the devil have in common?, or Bad habits to kick : mis-handling date / range queries Commented Apr 19, 2014 at 8:07

2 Answers 2

1

Try this:

Date startDate = (Date) data.get("startDate");//2014-04-01 
Date endDate = (Date) data.get("endDate");//2014-04-30
int pagesize=10;
String query = "select * from bill_details "
 + " where status=? "
 + " AND date(add_date) BETWEEN ? AND ?"
 + " order by add_date desc limit ?";
 ps = con.prepareStatement(query);
 ps.setString(1, dataStatus);
 ps.setDate(2, startDate);
 ps.setDate(3, endDate);
 ps.setInt(4, pagesize);
 System.out.println("Before execution");
 res = ps.executeQuery();
answered Apr 19, 2014 at 8:14
Sign up to request clarification or add additional context in comments.

1 Comment

this right one, try this code, your not set the value from the database,..this problem of your code
1

You're trying to concatenate a Date object to a String. Even if that works, then the date isn't surrounded with quotes in the query.

Use SimpleDateFormat to format the Date to a String.

Try this:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String query = "select * from bill_details "
 + " where status='active' "
 + " AND date(add_date) BETWEEN date('" + df.format(startDate) + "')"
 + " AND date('" + df.format(endDate) + "') " +
 + " order by add_date desc limit " + pagesize;
answered Apr 19, 2014 at 8:07

1 Comment

So date is treated as string

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.