您好必须检索数据库的一条记录,该记录应尽可能接近我的参数(日期(YYYY-MM)),请记住数据库(sql server)中的列是DATETIME,所以我需要格式化它以便我可以比较它,这是我一直在做的:
public Document findByDate(String date) throws GeneralException{
Document docs = new Document();
String d1 = date;
String delimiter = "-";
String[]temp = d1.split(delimiter);
try{
String sql = "SELECT TOP(1) * FROM Document WHERE issueDate >= '" + temp[1]+ temp[0] +"' AND issuedate < '"+ temp[1]+ temp[0] +"' ORDER BY issueDate DESC ";
ResultSet rs = this.executeQuery(sql);
while(rs.next()){
docs = (Document) this.build(rs);
}
if(docs != null){
return docs;
} else {
return docs = null;
}
} catch (SQLException ex){
throw new GeneralException(ex);
}
}
非常感谢你