O'Reilly logo

Hands-On High Performance with Spring 5 by Dinesh Radadiya, Prashant Goswami, Pritesh Shah, Subhash Shah, Chintan Mehta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Batch instead of PreparedStatement

Inserting a large amount of data into a database is typically done by preparing an INSERT statement and executing that statement multiple times. This increases the number of JDBC calls and impacts the performance. To reduce the number of JDBC calls and improve the performance, you can send multiple queries to the database at a time by using the addBatch method of the PreparedStatement object.

Let's look at the following example:

PreparedStatement ps = conn.prepareStatement("INSERT INTO ACCOUNT VALUES (?, ?)");for (n = 0; n < 100; n++) {    ps.setInt(accountNumber[n]);    ps.setString(accountName[n]);    ps.executeUpdate();}

In the preceding example, PreparedStatement is used to execute an INSERT statement multiple ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required