数据库SQL按日期查询问题
"select * from MRPII..forecastfile where Startdate = ' "
+ startDate + " ' and Enddate = ' " + endDate + " ' "; Startdate 和endDate都已经是java.sql.Date类型的,这样在数据库中查询对吗?但没有返回结果啊,
源程序:
public ArrayList queryByDateInter(Date startDate, Date endDate)
throws Exception {
DAO dao = new DAO();
ArrayList result = new ArrayList();
Forecastfile forecast = new Forecastfile();
try {
try {
dao.connection();
String sql = "select * from MRPII..forecastfile where Startdate = ' "
+ startDate + " ' and Enddate = ' " + endDate + " ' ";
Statement stmt = DAO.getConn().createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
forecast.setProductID(rs.getString(2));
forecast.setStartDate(rs.getDate(3));
forecast.setEndDate(rs.getDate(4));
forecast.setQuality(rs.getString(5));
forecast.setUnit(rs.getString(6));
forecast.setCreateMan(rs.getString(7));
forecast.setCreateTime(rs.getDate(8));
result.add(forecast);
} else {
try {
throw new QueryException(exceptions.isNotExist());
} catch (QueryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} finally {
dao.close();
}
} catch (SQLException e) {
try {
throw new QueryException(exceptions.getForecastInfo());
} catch (QueryException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return result;
}
[解决办法]
String sql = "select * from MRPII..forecastfile where Startdate = ' "
+ startDate + " ' and Enddate = ' " + endDate + " ' ";
改为
String sql = "select * from MRPII..forecastfile where Startdate > ' "
+ startDate + " ' and Enddate < " + endDate + "";