PostgreSQL JDBC: Querying Data
Summary: in this tutorial, you will learn how to query data from a table in the PostgreSQL database using JDBC API.
Steps for querying data
To query data from a table using JDBC, you follow these steps:
- Establish a database connection to the PostgreSQL server.
- Create an instance of the
Statement
orPreparedStatement
object. - Execute a statement to get a
ResultSet
object. - Process the
ResultSet
object. - Close the
Statement
&Connection
object by calling theirclose()
method.
If you use the try-with-resources statement, you don’t need to explicitly call the close()
method of the Statement
or Connection
object. It will automatically close these objects.
1) Establishing a database connection
Use the getConnection()
method of the DriverManager
class to establish a connection to the PostgreSQL server.
We’ll use the DB
class created in the connecting to the PostgreSQL server to connect to the PostgreSQL server.
2) Creating a Statement object
In JDBC, a Statement
object represents an SQL statement.
- First, create a
Statement
object from theConnection
object. - Then, execute the
Statement
object to get aResultSet
object that represents a database result set.
JDBC offers three types of Statement
objects:
Statement
: use the Statement to implement a simple SQL statement that has no parameters.PreparedStatement
: is the subclass of theStatement
class, which allows you to bind parameters to the SQL statement.CallableStatement
: extends thePreparedStatement
class that can execute a stored procedure.
3) Executing a query
To execute a query, you use one of the following methods of the Statement
object:
execute()
: Return true if the first object of the query is aResultSet
object. You can get theResultSet
by calling the methodgetResultSet()
.executeQuery()
: Return only oneResultSet
object.executeUpdate()
: Return the number of rows affected by the statement. Typically, you use this method for executing the INSERT, DELETE, or UPDATE statement.
4) Processing the ResultSet
Once having a ResultSet
object, you use a while loop to iterate over the result in the result set:
5) Closing a database connection
To close a Statement
or Connection
object, you call the close()
method explicitly in the finally
clause of the try...catch...finally
statement. This ensures that the resources are closed properly even if any exception occurs.
Starting from JDBC 4.1, you can use a try-with-resources statement to close ResultSet
, Statement
, and Connection
objects automatically.
Querying data examples
Let’s explore some examples of querying data from a table using JDBC.
1) Querying all rows from the products table
Define a new function findAll() in the ProductDB class to retrieve all rows from the products table:
How it works.
First, initialize an ArrayList
to store the returned products.
Second, construct a query that retrieves all rows from the products
table:
Third, open a database connection and create a Statement
object:
The try-with-resources will automatically close the Statement
and Connection
objects.
Fourth, execute the SELECT
statement by calling the executeQuery()
method:
Fifth, iterate over the result set, initialize the Product
object, and add it to the products
list:
Finally, return the products list:
The following shows how to use the findAll() method of the ProductDB class to retrieve all data from the products table and display each in the standard output:
Output:
2) Querying data with parameters
The following defines a method called findById() to find the product by id:
How it works.
First, construct a SELECT that selects a product by id and use the question mark (?) as the placeholder:
Second, open a connection to the database and create a PreparedStatement
object:
Third, bind the id to the statement:
Fourth, execute the statement using the executeQuery()
method of the PreparedStatement object:
Fifth, process the result set if the row with specified id exists and return the Product object:
The following shows how to use the findById()
in the main()
method of the Main() class to retrieve the product with id 1 from the products
table:
Output:
Summary
- Use the
executeQuery()
method of theStatement
orPreparedStatement
object to retrieve data from a table.