Difference between Statement and PreparedStatement
1. Statement :
It is used for accessing your database. Statement interface cannot accept parameters and useful when you are using static SQL statements at runtime. If you want to run SQL query only once then this interface is preferred over PreparedStatement.
Example –
//Creating The Statement Object Statement prep_statement = con.createStatement(); //Executing The Statement prep_statement.executeUpdate("CREATE TABLE STUDENT(ID NUMBER NOT NULL, NAME VARCHAR)");
2. PreparedStatement :
It is used when you want to use SQL statements many times. The PreparedStatement interface accepts input parameters at runtime.
Example –
//Creating the PreparedStatement object PreparedStatement prep_statement = con.prepareStatement("update STUDENT set NAME = ? where ID = ?"); //Setting values to place holders //Assigns "RAM" to first place holder prep_statement.setString(1, "RAM"); //Assigns "512" to second place holder prep_statement.setInt(2, 512); //Executing PreparedStatement prep_statement.executeUpdate();
Difference between Statement and PreparedStatement :
Statement | PreparedStatement |
---|---|
It is used when SQL query is to be executed only once. | It is used when SQL query is to be executed multiple times. |
You can not pass parameters at runtime. | You can pass parameters at runtime. |
Used for CREATE, ALTER, DROP statements. | Used for the queries which are to be executed multiple times. |
Performance is very low. | Performance is better than Statement. |
It is base interface. | It extends statement interface. |
Used to execute normal SQL queries. | Used to execute dynamic SQL queries. |
We can not use statement for reading binary data. | We can use Preparedstatement for reading binary data. |
It is used for DDL statements. | It is used for any SQL Query. |
We can not use statement for writing binary data. | We can use Preparedstatement for writing binary data. |
No binary protocol is used for communication. | Binary protocol is used for communication. |
Contact Us