Database interaction best practices
This section lists some basic rules that developers should be aware of when developing any applications. A failure to follow the rules will result in a poorly performing application.
Using Statement versus PreparedStatement versus CallableStatement
Choose between the Statement
, PreparedStatement
, and CallableStatement
interfaces; it depends on how you plan to use the interface. The Statement
interface is optimized for a single execution of an SQL statement, while the PreparedStatement
object is optimized for SQL statements that will be executed multiple times, and CallableStatement
is generally preferred for executing stored procedures:
Statement
: ThePreparedStatement
is used to execute normal SQL queries. It is preferred when a particular SQL query is to be executed only once. The performance of this interface is very low.
PreparedStatement
: ThePreparedStatement
interface is used to execute parametrized or dynamic SQL queries. It is preferred when a particular...