Java 好吧,Oracle查询它工作得很好,但PostgreSQL查询工作得不好。看起来问题出在查询上。 public List<CustomersObj> list(int firstRow, int rowCount, String sortFi
Java 好吧,Oracle查询它工作得很好,但PostgreSQL查询工作得不好。看起来问题出在查询上。 public List<CustomersObj> list(int firstRow, int rowCount, String sortFi,java,sql,postgresql,postgresql-9.3,Java,Sql,Postgresql,Postgresql 9.3,好吧,Oracle查询它工作得很好,但PostgreSQL查询工作得不好。看起来问题出在查询上。 public List<CustomersObj> list(int firstRow, int rowCount, String sortField, boolean sortAscending) throws SQLException { String SqlStatement = null; if (ds ==
好吧,Oracle查询它工作得很好,但PostgreSQL查询工作得不好。看起来问题出在查询上。
public List<CustomersObj> list(int firstRow, int rowCount, String sortField, boolean sortAscending) throws SQLException
{
String SqlStatement = null;
if (ds == null)
{
throw new SQLException();
}
Connection conn = ds.getConnection();
if (conn == null)
{
throw new SQLException();
}
int countrow = firstRow + rowCount;
String sortDirection = sortAscending ? "ASC" : "DESC";
// Oracle
// SqlStatement = "SELECT A.* "
// + " FROM (SELECT B.*, ROWNUM RN "
// + " FROM (SELECT Y.COMPONENTSTATSID, Y.NAME, Y.SERIALNUMBER, Y.WEIGHTKG, Y.ZONECAGE, Y.POWERWATT, Y.MANIFACTURECOMPANY, Y.UFORM, "
// + " Y.STATUS, Y.LOCATION, Y.HEATEMISIONSBTU, Y.PRODUCTIONENVIRONMENT, Y.STANDARTLIFETIME, Y.OPERATINGHAMIDITYRANGE, "
// + " Y.OPERATINGSYSTEM, Y.DATEDEPLOYED, Y.INTERFACETYPE, Y.TYPE, Y.COOLINGCAPACITYBTU, Y.DATEADDED, Y.DESCRIPTION "
// + " FROM COMPONENTWEIGHT X, COMPONENTSTATS Y WHERE X.COMPONENTSTATSID = Y.COMPONENTSTATSID AND Y.COMPONENTTYPEID = 3300 "
// + " ORDER BY %S %S) B "
// + " WHERE ROWNUM <= ?) A "
// + " WHERE RN > ?";
// postgresql
SqlStatement = "SELECT * FROM CUSTOMERS ORDER BY %S %S offset ? limit ? ";
String sql = String.format(SqlStatement, sortField, sortDirection);
PreparedStatement ps = null;
ResultSet resultSet = null;
List<CustomersObj> resultList = new ArrayList<>();
try
{
conn.setAutoCommit(false);
boolean committed = false;
ps = conn.prepareStatement(sql);
ps.setInt(1, countrow);
ps.setInt(2, firstRow);
resultSet = ps.executeQuery();
resultList = ProcessorArrayList(resultSet);
conn.commit();
committed = true;
}
finally
{
ps.close();
conn.close();
}
return resultList;
}
LIMIT { count | ALL } OFFSET start
int countrow = firstRow + rowCount;
ps.setInt(1, countrow);
ps.setInt(2, firstRow);
ps.setInt(1, firstRow - 1);
ps.setInt(2, rowCount);
int countrow = firstRow + rowCount;
SqlStatement = "SELECT * FROM CUSTOMERS ORDER BY %S %S offset ? limit ? ";
ps = conn.prepareStatement(sql);
ps.setInt(1, countrow);
ps.setInt(2, firstRow);
To retrieve the first page -> `LIMIT 50 OFFSET 0` [rows 0 to 49]
To retrieve the second page -> `LIMIT 50 OFFSET 50` [rows 50 to 99]
...
To retrieve the nth page -> `LIMIT 50 OFFSET (n - 1)*50`
ps.setInt(1, firstRow); // Assign firstRow to OFFSET
ps.setInt(2, rowCount); // Assign rowCount to LIMIT
YES -> ps.setInt(1, firstRow);
NO -> ps.setInt(1, firstRow - 1);