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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.