所以我有这个查询使用MS SQL Server上的DATEDIFF
函数?获取日期差异的返回值?
SELECT DATEDIFF(DAY,(select StartDate from CarOrder where OrderID= 59),(select EndDate from CarOrder where OrderID= 59))
这项工作很好,结果是10,但我不知道如何获得的返回值,并用它在这个函数的Java
public int getDateDiff(int OrderID){
Connection conn = DBConnection.getConnection();
int datediff;
String getdiffSQL = "SELECT DATEDIFF(DAY,(select StartDate from CarOrder where OrderID = ?) ,(select EndDate from CarOrder where OrderID= ?))";
try {
PreparedStatement pstm = conn.prepareStatement(getdiffSQL);
pstm.setInt(1, OrderID);
pstm.setInt(2, OrderID);
int nRows = pstm.executeUpdate();
return nRows;
} catch (SQLException ex) {
System.out.println("Error: " + ex.getMessage());
} finally {
DBConnection.closeConnection(conn);
}
}
我希望它返回日期DIFF号码,它是一个我可以在java中使用的变量,我该怎么做? 谢谢。
你能澄清一下你上面的代码到底出了什么问题吗? –
没有任何错误,我只需要从datediff得到返回值,并将它变成我可以在java中使用的变量。 –
与你的问题没有关系,但查询会更好,因为'SELECT DATEDIFF(DAY,StartDate,EndDate) FROM CarOrder WHERE OrderID =?' –