O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

The true usefulness of PreparedStatement shines because of its ability to accept parameters--the input values that substitute (in the order of appearance) the ? symbol. Here's an example of this:

traverseRS("select * from enums");System.out.println();try (Connection conn = getDbConnection()) {  String[][] values = {{"1", "vehicle", "car"},                       {"2", "vehicle", "truck"}};  String sql = "insert into enums (id, type, value) values(?, ?, ?)");  try (PreparedStatement st = conn.prepareStatement(sql) {    for(String[] v: values){      st.setInt(1, Integer.parseInt(v[0]));      st.setString(2, v[1]);      st.setString(3, v[2]);      int count = st.executeUpdate();      System.out.println("Update count = " + count);    }  }} catch (Exception ex) { ex.printStackTrace() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required