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.