Ich möchte Bereich oder Zeilen aus PostgreSQL-Tabelle auswählen. Ich habe versucht, diesen Code:Wählen Sie einen Bereich von Zeilen aus PostgreSQL-Tabelle
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;
}
Aber wenn ich die Paginierung verwende ich unterschiedliche Anzahl oder Zeilen erhalten. Wenn ich die Abfrage von Oracle verwende, funktioniert es gut. Aber wenn ich die Abfrage von PostgreSQL verwenden möchte, bekomme ich auf jeder paginierten Seite ein unterschiedliches Ergebnis. Kannst du ein Ergebnis geben, wie ich das beheben kann?
ich org.postgresql.util.PSQLException: ERROR: OFFSET nicht negativ sein muss –
@Peter also überprüfen, ob die 'firstRow' wirklich von 1 startet Wenn es bei Null beginnt, brauchen Sie nicht die subtrahieren 1. – RealSkeptic
ist es 1. Aus irgendeinem Grund funktioniert es nicht richtig. Next-Button und Back funktionieren nicht richtig. –