I am working on an inherited large Java code base where many disparate methods make queries to the database in maddeningly different ways; as I've been debugging and standardizing everything, most of the code I write ends up looking like this:
log.info("Audit-required logging for query: "+SOME_QUERY);
log.info("Ditto for each argument: "+parameter+" "+otherParameter+ ...);
ps = conn.prepareStatement(SOME_QUERY);
ps.setString(1, aString);
ps.setString(2, anotherString);
// ...
ps.setString(14, yetAnotherString);
rs = ps.executeQuery();
log.debug("Query executed: "+SOME_QUERY);
I hate that I have to write down the query thrice and the parameters twice (plus do a setString() for each one) — it's a recipe for future bugs when doing maintenance. I'd rather put all that inside a (static) general-purpose method that would allow me to just say everything once (plus future-proof the code base, in case some other action is needed... like, say, a different kind of logging is required by the legal department or a new kind of error handling is asked for). Something like this:
public static PreparedStatement fullyPrepare(final Connection conn, final String query, final String... arguments) { ... }
which I'd then call with a single sentence (instead of an entire code block every time):
ps = fullyPrepare(conn, CONSTANT_FOR_THIS_QUERY, parameter, otherParameter, ...);
I've, however, found resistance to this, based on the idea that "it would be bad practice". I've been trying to read up about this, but I can't find anything saying whether returning a PreparedStatement from a method that prepares it would be a good or a bad practice (as opposed to, say, the handling of ResultSet objects, like in the thread Is it Ok to Pass ResultSet?).
Why would my intended PreparedStatement preparator be a bad idea?