我有用於JSF分頁這個Java代碼:從SQL查詢檢索的記錄範圍甲骨文
public List<ActiveSessionObj> list(int firstRow, int rowCount, String sortField, boolean sortAscending) throws Exception {
String SQL_LIST_BY_ORDER_AND_LIMIT = "SELECT * FROM ACTIVESESSIONSLOG ORDER BY ? ? LIMIT ?, ?";
if (ds == null) {
throw new SQLException();
}
String sortDirection = sortAscending ? "ASC" : "DESC";
String sql = String.format(SQL_LIST_BY_ORDER_AND_LIMIT, sortField, sortDirection);
Connection conn = ds.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<ActiveSessionObj> dataList = new ArrayList<ActiveSessionObj>();
try {
conn.setAutoCommit(false);
boolean committed = false;
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, sortField);
preparedStatement.setString(2, sortDirection);
preparedStatement.setInt(3, firstRow);
preparedStatement.setInt(4, rowCount);
resultSet = preparedStatement.executeQuery();
/* take the result from the SQL query and insert it into Array List collection */
dataList = ActiveSessionsArrayList(resultSet);
} catch (SQLException e) {
throw new Exception(e);
} finally {
conn.close();
}
return dataList;
}
我用這個SQL語句產生的ArrayList:
SELECT * FROM ACTIVESESSIONSLOG ORDER BY ? ? LIMIT ?, ?
可以這樣SQL查詢用於Oracle?或者這是MySQL的具體?
最良好的祝願
第一個例子(Case1)將**不工作。您不能在聲明中將rownum和訂單合併到同一級別。 – 2012-04-08 13:58:10
@a_horse_with_no_name:先生它正在爲我工作,在案件1當我通過聘用訂購時,如果你想我會給你一個例子 – 2012-04-08 14:02:57
結果將**不一致,因爲rownum值被分配*之前*任何訂購已完成。看到我的例子在這裏:http://sqlfiddle.com/#!4/bede9/1 – 2012-04-08 14:05:18