Summary: in this tutorial, you will learn how to insert data into a table in the PostgreSQL database using JDBC.
Inserting one row into a table
We’ll use the products table from the sales database for the demonstration.
Defining a Product class
The following creates Product.java file and defines the Product class with three properties id, name, and price. These properties correspond to the columns in the products table:
Defining a ProductDB class
The following creates a new file called ProductDB.java and defines the ProductDB class:
The add() method inserts a new row into the products table.
How it works.
First, initialize an INSERT statement:
The question mark (?) is a placeholder that will be replaced by the actual values later.
Second, open a connection to the sales database on the local PostgreSQL server using the DB class:
Third, create a PreparedStatement object by calling the preparedStatement() method:
The Statement.RETURN_GENERATED_KEYS argument instructs the PreparedStatement object to return the generated id key of the product.
Fourth, bind the values to the statement:
Behind the scenes, the PreparedStatement will validate the values and bind them to the placeholders (?) accordingly.
Fifth, execute the INSERT statement and return the number of inserted rows by calling the executeUpdate() method of the PreparedStatement object:
Sixth, retrieve the inserted id and return it:
If any SQLException occurs, display the detail of the exception in the catch block.
Since the Connection and PreparedStatement objects are created in the try-with-resources statement, they will be automatically closed.
Adding a product
The following main() method uses the add() method of the ProductDB class to insert a new row into the products table:
If you run the program, it’ll show the following output:
Verify the insert
Connect to the sales database and retrieve the data from the products table to verify the insert:
Output:
Inserting multiple rows into a table
Define a new method add() that accepts a list of Product objects and inserts them into the products table:
How it works.
First, initialize an INSERT statement:
Second, open a connection and create a PreparedStatement object:
The try-with-resources statement ensures that the PreparedStatement and Connection objects will be closed automatically.
Third, iterate over the Product in the Products list, bind the values to the statement, and add the statement to a batch for insertion:
Finally, execute insert statements in batch by calling the executeBatch() method of the PreparedStatement object:
Adding multiple products
The following shows how to use the ProductDB class to add multiple products to the products table:
Verify the inserts
Connect to the sales database and query data from the products table to verify the inserts:
Output:
The output indicates the new add() method added nine rows to the products table successfully.
Summary
Call the executeUpdate() method of the PreparedStatement object to execute the INSERT statement to insert a new row into a table.
Use the addBatch() and the executeBatch() methods of the PreparedStatement object to execute batch inserts.