Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the "Statement" Object required if the "prepared" statement is superior?

Tags:

java

sql

jdbc

The Statement object, as I understand, is not to be used in developing Enterprise applications as it has all sorts of issues in a good application. Why has it not been deprecated yet in newer versions?

like image 675
Rahul_tcs Avatar asked Mar 22 '23 07:03

Rahul_tcs


2 Answers

Statement is a perfectly fine interface. What's bad is creating queries by concatenating strings together, especially strings containing user input. If you're only issuing constant queries containing no variables, the simple Statement interface serves perfectly well.

like image 62
Ernest Friedman-Hill Avatar answered Apr 26 '23 04:04

Ernest Friedman-Hill


Statement

Use for general-purpose access to your database. Useful when you are using static SQL statements at runtime. The Statement interface cannot accept parameters.

The use of a Statement in JDBC should be 100% localized to being used for DDL (ALTER, CREATE, GRANT, etc) as these are the only statement types that cannot accept BIND VARIABLES.

PreparedStatement

Use when you plan to use the SQL statements many times. The PreparedStatement interface accepts input parameters at runtime.

PreparedStatements or CallableStatements should be used for EVERY OTHER type of statement (DML, Queries). As these are the statement types that accept bind variables.

like image 29
Yellow Flash Avatar answered Apr 26 '23 04:04

Yellow Flash