In the case of a PreparedStatement object, as the name implies the application program prepares a SQL statement using the java.sql.Connection.preapreStatement() method. The prepareStatement() method takes an SQL string, which is passed to the underlying DBMS. The DBMS goes through the syntax run, query plan optimization and the execution plan generation stages, but does not execute the SQL statement. Possibly, it returns the handle to the optimized execution plan that the JDBC driver stores internally in the PreparedStatement object.
The method of the PreparedStatement object are executeQuery(), executeUpdate(), and execute(). Notice that the methods do not take any parameters. They are just calls to the underlaying DBMS to perform the already optimized SQL statement.
One of the major features of a PreparedStatement is that it can handle IN types of parameters. The parameters are indicated in an SQL statement by placing the ‘?’ (question mark) as the parameter marker instead of actual values.
In the case of the PreparedStatement, the driver actually sends only the execution plan ID and the parameters to the DBMS. This results in less network traffic and is well-suited for Java application on the Internet. The PrepatedStatement should be used when you need to execute the SQL statement many times in a Java application.
But remember, even though the optimized execution plan is available during the execution of a Java program, the DBMS discards the execution plan at the end of the program. So, the DBMS must go through all of the steps of creating an execution plan every time the program runs. The PreparedStatement object achieves faster SQL execution performance than the simple Statement object, as the DBMS does not have to run through the steps of creating the execution plan.



No comments:
Post a Comment