为什么此查询不返回任何值。我的数据库中有值在该范围内
值输入
Date startDate = (Date) data.get("startDate");//2014-04-01
Date endDate = (Date) data.get("endDate");//2014-04-30
int pagesize=10;
sql查询是
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();
答案 0 :(得分:1)
您正在尝试将Date
对象连接到String
。即使这样可行,那么日期也不会被查询中的引号包围。
使用SimpleDateFormat将日期format
改为String
。
试试这个:
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;
答案 1 :(得分:1)
试试这个:
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();