Create JdbcTemplate Beans

Example code is available on github.com/vaadin-on-grails/jdbc-template.

In this article, we will show how to define beans in order to start working with spring-jdbc.

Step 1

Add dependency to spring-jdbc into BuildConfig.groovy.

dependencies {
    compile 'org.springframework:spring-jdbc:4.0.6.RELEASE'
}

Step 2

We can use JdbcTemplate to directly execute SQL queries, but if there will be inputs from users, we have to use ? as a parameter placeholder or rather use NamedParameterJdbcTemplate to prevent SQL injection.

import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate

beans = {

    jdbcTemplate(JdbcTemplate, ref('dataSource'))

    namedJdbcTemplate(NamedParameterJdbcTemplate, ref('dataSource'))
}

Last updated