所以我有這個查詢使用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 =?' –