You are here:   HomeJava ProgrammingJDBCJDBC - PreparedStatement Interface (Handling Database Queries Dynamically)
by Dinesh Thakur Category: JDBC

PreparedStatement interface extends the Statement interface so it has access to all the methods of Statement interface. It is used to execute precompiled parameterized SQL statements.

This helps statements to execute much faster thus increases the efficiency of the program. It allows you to specify SQL query, in which unknown values are replaced by '? '. The user provides the unknown values later at the run-time. The setString () and set Int () methods can be used to specify the values for the parameters. To understand the concept of prepared statement, consider the following statements.

PreparedStatement ps = con.prepareStatement("INSERT INTO Student VALUES(?, ? , ? ) ") ;

ps.setString(l, "A010");  //assigns value to first attribute

ps.setint(2, 90);              //assigns value to second attribute

ps.setString(3, "A");      //assigns value to third attribute


The values can be assigned to the corresponding attributes through variable name also instead of using literal values representing index value. Like, set String () and set Int () methods, there are various methods for setting value of other data type. Some of these are setBoolean (), setFloat (), setByte (), setDate (),etc. The method void clearParameters () can be used to clear all the values held currently in the parameters.

What's New and Popular