Creating an instance of JdbcTemplate from java.sql.Connection

I want to get JdbcTemplatein my java code. I already have a worker java.sql.Connection. To create a new one JdbcTemplate, an instance of the interface is usually required javax.sql.DataSource.

How can I get a new one JdbcTemplatefrom an existing one java.sql.Connection?

+5
source share
2 answers

Technically you can use SingleConnectionDataSource

new JdbcTemplate(new SingleConnectionDataSource(connection, false))

However, this is not entirely appropriate if, for example, for unit tests.

It's better to use full-featured DataSourceand wired stuff using spring.

+21
source

, JdcbTemplate - Spring; JDK. JdbcTemplate.

- JdbcTemplate bean Spring; , .

+2

All Articles