0

In Oracle I can have an SQL string like this

select * from table where rownum <= ?

Then in a PreparedStatement I can set the rownum as a parameter.

How does it work with PostgreSQL JDBC (Java) with the LIMIT clause? Doing the following won't let me set a parameter:

select * from table limit ?
1
  • 1
    Which version of the PostgreSQL JDBC driver are you using, and which version of PostgreSQL? Commented Oct 21, 2024 at 9:42

3 Answers 3

2

Apparently you are using such an old version of the JDBC driver (version 7.x.x or lower), in which the LIMIT parameterization is not supported. This version was released about twenty years ago and is completely outdated. I recommend that you switch to using a new version of the driver as soon as possible.

In PostgreSQL, you can use LIMIT to limit the number of records retrieved.

In the following code

PreparedStatement statement = connection.prepareStatement("SELECT * FROM users LIMIT ?"));
statement.setInt(1, 10);

You create a prepared statement and set the LIMIT parameter to get the first 10 records.

For more information, see the PostgreSQL documentation.

Sign up to request clarification or add additional context in comments.

8 Comments

As far as I'm aware, LIMIT doesn't accept a parameter in PostgreSQL.
@MarkRotteveel The question was also about JDBC (Java Database Connectivity) driver usage, and Java programming language. The setting of parameter is by using the JDBC API. The JDBC API's PreparedStatement exactly allows this - to set the limit value via a parameter.
@MarkRotteveel I couldn't find a way to make LIMIT not accept the parameter. What version of JDBC driver are you using?
@prasad_ The PostgreSQL JDBC driver generally only supports setting parameters if the server also supports parameters in that position.
@AndreySmelik I'm not the OP. Right now, I'm simply going by what the OP states, and two other answers confirm doesn't work.
|
2

How does it work with Postgres JDBC (Java) with the LIMIT clause? Doing the following won'e let me set a parameter:

select * from table limit ?

This expands on the answer by @Andrey Smelik.

Using PostgreSQL JDBC driver (access database using Java programming), you can use the SQL LIMIT clause to limit the number of rows returned by the query.

This also allows set the prepared statement's (of JDBC API) parameter to specify the "limit". For example:

Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "mydb", "***");
final String SQL = "SELECT name FROM table LIMIT ?";

try (PreparedStatement pst = conn.prepareStatement(SQL)) {

    pst.setInt(1, 5); // limit the number of rows by 5
    ResultSet rs = pst.executeQuery();
    while (rs.next())
        System.out.println("Column data: " + rs.getString("name"));
}

// ... close connection, etc.

The above code prints five rows of name column value from the database table table.

7 Comments

The question of the OP explicitly states that this doesn't work, so how can this possibly be the answer?
@MarkRotteveel I dont follow your comment. I had tried the code using Postgres JDBC driver (postgresql-42.7.3.jar) accessing Postgres database v14 and Java SE 17 - it runs fine.
The OP clearly says they did this, and it didn't work. So just telling them they should do what they already did won't answer the question.
@MarkRotteveel Then the question was misleading.
Well, I tested it against 17 and 12.20, and it works, so it looks my remembrance was wrong, as is the OP and the other two answers.
|
0

It is not possible use a ? placeholder in a LIMIT clause with your current database and driver (q.v. the other answers and comments which indicate that your current query might work). If you absolutely needed to do this from SQL, one workaround might be to use ROW_NUMBER() with a subquery. In other words, change this:

SELECT * FROM yourTable ORDER BY some_col LIMIT ?

to this:

SELECT *
FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY some_col) rn
    FROM yourTable
) t
WHERE rn < ?;

However, in practice, if you are using JPA, you could use the various JPA methods to programmatically limit the size of the result set.

4 Comments

If this doesn't work, why do two other answers state it does work?
What doesn't work in my answer?
You state that it is not possible to use ? in LIMIT, two other answer state it does work. And a quick test against PosgreSQL 17 with the 42.7.3 JDBC driver confirms it works.
@Mark I added a caveat to my answer. Assuming the OP's db/driver combo does not allow a placeholder in LIMIT, then my answer as originally stated is valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.