我收到此错误,但我不明白为什么我在工作台上尝试了该语句并且它起作用了。我也在寻找改进我的日期减去代码基本上我从表格中得到最后期限,我正在从今天的日期中减去。public boolean checkLessThanWeek(User userWhoWantsToExtendDeadline,BorrowedBook book) throws ParseException { try { Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT Deadline FROM library_students.borrowedcopies WHERE LenderToID =" + "'" + userWhoWantsToExtendDeadline.getId()+ "'"+"And Barcode="+"."+book.getbookID()); if (rs.next()) { SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); Date deadline = format.parse(rs.getString(1)); Date date=new Date(); String today=format.format(date.getDate()); date = format.parse(today); long milliseconds=date.getTime()-deadline.getTime(); long days = milliseconds / (1000 * 60 * 60 * 24); System.out.print(days); } rs.close(); return true; } catch (SQLException ex) { ex.printStackTrace(); return false; }}
1 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
这很可能是因为book.getbookID()返回 null 并且由于某种原因.您在 Sql 中它前面有一个点 ()。除此之外,最好使用参数化的 Sql 语句?,并且基本的错误处理也是一件好事。
if (userWhoWantsToExtendDeadline == null || book == null || book.getbookID() == null {
//throw exception perhaps or log?
return false;
}
String query = "SELECT Deadline FROM library_students.borrowedcopies WHERE LenderToID = ? AND Barcode = ?";
Statement stmt = con.createStatement(query);
stmt.setInt(1, userWhoWantsToExtendDeadline.getId());
stmt.setString(2, book.getbookID());
ResultSet rs = stmt.executeQuery();
请注意,我假设您的第一个参数是 anint而第二个参数是 aString但当然可能需要更改
添加回答
举报
0/150
提交
取消