I want to obtain a JdbcTemplate
in my Java code. I've already got a working java.sql.Connection
. To create a new JdbcTemplate
it would normally need an instance of the javax.sql.DataSource
interface.
Is it somehow possible to obtain a new JdbcTemplate
from an existing java.sql.Connection
?
Technically, you can, using SingleConnectionDataSource
new JdbcTemplate(new SingleConnectionDataSource(connection, false))
However, this is not quite advisable, unless for unit-tests for example.
You'd better use a full-featured DataSource
and wire things using spring.