title: OFFSET N FETCH FIRST M ROWS with JDBC and PostgreSQL not working
body: <p>I'm trying to make a query using JDBC and PostgreSQL, but I'm facing a weird situation that I can't find in any documentation.</p>
  
  <p>The following query works if I execute it through pgAdmin and H2 (which I'm using for unit tests for my application), but I'm getting a syntax error if I execute it through JDBC:</p>
  
  <p><code>Queries.SELECT_SQL</code></p>
  
  <pre><code>SELECT columns 
    FROM Table 
    LEFT JOIN TableToJoin1 
    LEFT JOIN TableToJoin2 
    LEFT JOIN TableToJoin3 
    JOIN TableToJoin4
  </code></pre>
  
  <hr>
  
  <p><code>Queries.ENDING_PAGING_STATEMENT_SQL</code></p>
  
  <pre><code>OFFSET ? ROWS FETCH FIRST ? ROWS ONLY
  </code></pre>
  
  <hr>
  
  <p>The <code>SELECT</code> query is correct and works fine using any means, the problem is when I get both queries together, i.e., the <code>SELECT</code> statement along with <code>OFFSET</code> and <code>FETCH FIRST</code>.</p>
  
  <p>This is how I'm executing the query through <code>JDBC</code>:</p>
  
  <pre><code>// Receive offset and limit as argument.
  try (final PreparedStatement selectStatement = connection
          .prepareStatement(Queries.SELECT_SQL + Queries.ENDING_PAGING_STATEMENT_SQL)) {
  
    selectStatement.setInt(Queries.PAGING_ENDING_STATEMENT_OFFSET_ARGUMENT_POSITION, offset);
    selectStatement.setInt(Queries.PAGING_ENDING_STATEMENT_LIMIT_ARGUMENT_POSITION, limit);
  
    final ResultSet resultSet = bookSelectStatement.executeQuery();
    ...
  }
  </code></pre>
  
  <p>Here is the message of the exception that's being thrown:</p>
  
  <pre><code>org.postgresql.util.PSQLException: ERROR: syntax error at or near "$2"
  </code></pre>
  
  <hr>
  
  <p>If I call <code>selectStatement.toString()</code> for debugging, I receive the query exactly as I expect.</p>
  
  <p>If I change <code>OFFSET N ROWS FETCH FIRST M ROWS ONLY</code> by <code>OFFSET N LIMIT M</code> it will work through JDBC without any problem.
  The simplest solution would make that simple change above, but part of my task is to make a query using this <code>FETCH FIRST M ROWS ONLY</code> statement.</p>
  
  <p>So, can you guys help me to see if I'm doing it wrong, or if there's no way of using <code>FETCH FIRST</code> on JDBC for PostgreSQL at all? With some examples or some references to any documentation that would help.</p>
  
  <p>I noticed that <code>FETCH FIRST</code> doesn't exist on <a href="https://www.postgresql.org/docs/10/static/queries-limit.html" rel="nofollow noreferrer">PostgreSQL Documentation: Queries - Offset and Limit</a>, but since it works through pgAdmin I don't know why I should have any problem using JDBC.</p>
  
  <p>Thank you so much!</p>