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
?