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.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.