In JDBC, we have the following three Statement objects.
- Statement
- PreparedStatement
- CallableStatement
We have done with Statement, now let us know PreparedStatement.
PreparedStatement:
This is similar to the Statement, but the difference is PreparedStatement improves the performance. The syntax for creating PreparedStatment object is as follows:
Fig: syntax for creating PreparedStatement object. |
This prepareStatement(String SQL) is present in Connection Interface. So, it should be called with Connection class object(object of a class which implements Connection Interface). This method takes the SQL query as a parameter, and it returns a PreparedStatement object. The following program shows how to use work with PreparedStatement.
Fig: PreparedStatement example |
In the above program we are sending the query to the database using PreparedStatement. In the query you can see the POSITIONAL PARAMETERS, which represents the columns in the emp table. We can give values for those columns using the setXXX(xxx represents the datatypes) methods.