0

I know the way how preparation of statements looks like in JDBC, something like:

String query = "update users set num_points = ? where first_name = ?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt(1, 6000);
preparedStmt.setString(2, "Fred");

What I do not like about this, is that every variable should be set separately:

preparedStmt.setInt(1, 6000);
preparedStmt.setString(2, "Fred");

Is there any bulk method? Something like:

preparedStmt.set(List_of_vals);

And is possible to address fields not by an index, but by a name? Something like:

String query = "update users set num_points = :num_points where first_name = :first_name";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.bind(":num_points", 6000);
preparedStmt.bind(":first_name", "Fred");
// ^^^ it would be great, if I did not have to switch between 
//     setInt, setString, etc

But what I do not like most of all, is that for each variable I should specify its own setter, depending on the type, like setInt, setString. I wish there was a more generic method like bind(name, value). Does it exist in JDBC?

Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • regarding named bind vars, see this http://stackoverflow.com/questions/2309970/named-parameters-in-jdbc – Sharon Ben Asher Nov 01 '15 at 15:32
  • @Tunaki. Named parameters in JDBC - is only a one third of my question. – Jacobian Nov 01 '15 at 15:43
  • @Jacobian Actually, no. You can't do what you want using JDBC. The answer in the dupe target answers all of your questions (notice the `addValue` which mimics your `bind`). – Tunaki Nov 01 '15 at 15:44

0 Answers0