25.11. PreparedStatements

Interface PreparedStatement enables you to create compiled SQL statements that execute more efficiently than Statement objects. PreparedStatements also can specify parameters, making them more flexible than Statements. Programs can execute the same query repeatedly with different parameter values. For example, in the books database, you might want to locate all book titles for an author with a specific last name and first name, and you might want to execute that query for several authors. With a PreparedStatement, that query is defined as follows:

PreparedStatement authorBooks = connection.prepareStatement(
   "SELECT lastName, firstName, title " +
   "FROM authors INNER JOIN authorISBN " +
      "ON authors.authorID=authorISBN.authorID ...

Get Java™ How to Program, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.