我尝试用java.sql.Date类,将获得的当天时间与图书进货时间相减,我用的是DB2数据库,其中进货时间是DATE类型的,但是当我用java.sql.Date类型的时间减去进货时间,说减法函数或操作符的操作数数据不是数字,时间不可以相减么??急啊,如何解决??? public ResultSet newbookQuery(int choice) { String sql; // get the current time using SQL statement java.sql.Date current = new java.sql.Date(new java.util.Date().getTime()); if(choice == 0) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE=0"; if(choice ==1) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=1"; if(choice == 2) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=2"; if(choice == 3) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=3"; if(choice == 4) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=4"; if(choice == 10) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=10"; else sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=30"; try { stmt = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(); while(rs.next()) { System.out.println(rs.getString(1)+","+rs.getString(2)); } return rs; }catch(SQLException e) {e.printStackTrace();return null;} } 错误: java.sql.SQLException: [IBM][CLI Driver][DB2/NT] SQL0402N 算术函数或运算 "-" 的 操作数的数据类型不是数字。 SQLSTATE=42819
public ResultSet newbookQuery(int choice) {
String sql;
// get the current time using SQL statement
java.sql.Date current = new java.sql.Date(new java.util.Date().getTime()); if(choice == 0) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE=0";
if(choice ==1) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=1";
if(choice == 2) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=2";
if(choice == 3) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=3";
if(choice == 4) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=4";
if(choice == 10) sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=10";
else sql = "SELECT * FROM BOOK WHERE " + current + "-IMPORTDATE<=30";
try {
stmt = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery();
while(rs.next()) {
System.out.println(rs.getString(1)+","+rs.getString(2));
}
return rs;
}catch(SQLException e) {e.printStackTrace();return null;}
}
错误:
java.sql.SQLException: [IBM][CLI Driver][DB2/NT] SQL0402N 算术函数或运算 "-" 的
操作数的数据类型不是数字。 SQLSTATE=42819
下为sqlserver中where DATEDIFF(day, IMPORTDATE, getdate()) > 5建议到数据库版问!