convenience methods
Lance Andersen
lance.andersen at oracle.com
Thu Nov 23 13:17:15 UTC 2017
Hi Lukas,
If you have some specific convenience methods to suggest we can consider them
> On Nov 22, 2017, at 6:28 AM, Lukas Eder <lukas.eder at gmail.com> wrote:
>
> Hi Douglas,
>
> I think that convenience methods are really very very helpful. jOOQ has
> tons of them, for instance, because that really greatly reduces boiler
> plate code on the call site and makes using the API much more effective and
> fun. This also helps increase adoption.
>
> One of the main reasons why Spring's JdbcTemplate, or Apache DbUtils, or
> the simpler parts of the jOOQ API exists is the fact that the (synchronous)
> JDBC API doesn't have any such methods. For instance, it would make total
> sense to have
>
> public interface Connection {
> default PreparedStatement prepareStatement(String sql, Object...
> bindings) {
> PreparedStatement s = prepareStatement(sql);
> for (int i = 0; i < bindings.length; i++)
> s.setObject(i + 1, bindings[i]);
> }
>
> default int executeUpdate(String sql, Object... bindings) {
> try (PreparedStatement s = prepareStatement(sql, bindings)) {
> return s.executeUpdate();
> }
> }
The downside to this method is the PreparedStatement is closed after each execution and depending on the driver, the overhead might negate the ease of use.
But please feel free to propose methods you believe would be worthwhile and I will be happy to discuss and consider.
Best
Lance
> }
>
> I mean, who really enjoys counting bind indexes manually and sending a very
> simple static INSERT statement with 10 values to the server by setting each
> bind variable individually?
>
> This convenience stuff has been dearly missing from the very first version
> of JDBC :) (granted, it would have been difficult to add, without default
> methods). There are many other possible convenience methods, including:
>
> public interface ResultSet {
> default Object[] get() {
> Object[] result = new Object[getMetaData().getColumnCount()];
>
> for (int i = 0; i < result.length; i++)
> result[i] = getObject(i);
>
> return result;
> }
>
> default void forEach(Consumer<Object[]> consumer) {
> while (next())
> consumer.accept(get());
> }
> }
>
> I could go on and on, if you're interested. In fact, consider this a
> feature request! :)
<http://oracle.com/us/design/oracle-email-sig-198324.gif>
<http://oracle.com/us/design/oracle-email-sig-198324.gif> <http://oracle.com/us/design/oracle-email-sig-198324.gif>
<http://oracle.com/us/design/oracle-email-sig-198324.gif>Lance Andersen| Principal Member of Technical Staff | +1.781.442.2037
Oracle Java Engineering
1 Network Drive
Burlington, MA 01803
Lance.Andersen at oracle.com <mailto:Lance.Andersen at oracle.com>
More information about the jdbc-spec-discuss
mailing list