Shubham Uniyal
Shubham Uniyal

Reputation: 119

Difference between newObject[]{...parameters..} and PreparedStatementSetter Interface implementation in Spring JDBC?

What is the difference between passing parameters to SQL statements using the new Object[]{parameters} and using the PreparedStatementSetter Interface?

Both provide values to the placeholders in our sql query statement so why should we actually go and provide such long implementation of PreparedStatementSetter Interface when we can just use new Object[] to pass the parameters??

Upvotes: 1

Views: 118

Answers (1)

Andreas
Andreas

Reputation: 159086

The difference is convenience.

query(String sql, Object[] args, int[] argTypes, ...) and query(String sql, Object[] args, ...) are both convenience methods, that internally calls query(String sql, PreparedStatementSetter pss, ...).

Same as query(..., RowCallbackHandler rch) and query(..., RowMapper<T> rowMapper) both are convenience methods, that internally calls query(..., ResultSetExtractor<T> rse).

Convenience methods are nice, because they make the common action a lot easier to do. Providing access to the underlying method allow you to do uncommon actions too, if the need arrives.

As a library, where you never know what the caller might need, it is very appropriate to provide the caller with options to choose from.

Most people will never need the PreparedStatementSetter or ResultSetExtractor versions, so just ignore them, but do remember that they exist, in case you ever have to need.

Upvotes: 3

Related Questions