Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PreparedStatement batch and Statement batch

Tags:

java

jdbc

I'm learning JDBC at the moment and I already know the difference between PreparedStatement and Statement. It is that PreparedStatement is precompiled and allows you to set parameters, but I was asked a question on a job interview about difference between PreparedStatement batch and Statement batch?

like image 973
Юра Арабчук Avatar asked Sep 14 '25 06:09

Юра Арабчук


1 Answers

The difference between batch execution of a Statement and PreparedStatement, is that a Statement batch can contain different statements (as long as they are statements that do not produce a result set), for example a single batch can contain all kinds of inserts into various tables, deletes, updates, and - not in all JDBC driver implementations AFAIK - even DDL statements.

On the other hand, a PreparedStatement batch execution concerns a single statement, and the batch contains the multiple sets of parameter values to execute for that statement. That is, each batch entry defines the values to use for the parameters of the prepared statement.

In short:

  • Statement: batch can contain a lot of different statements
  • PreparedStatement: single statement, multiple sets of parameter values
like image 115
Mark Rotteveel Avatar answered Sep 17 '25 01:09

Mark Rotteveel