Java + Spring JDBC Template + Gradle Example

This is a very basic getting started Spring JDBC Template example, you can download the code from GitHub,

GitHub Repo Link:


  • Java 8+
  • Gradle
  • IDE - Eclipse/IntelliJ/VS Code
  • MySQL/Oracle/Postgres DB Connector jar
  • Spring Core + Context + JDBC dependencies.

Setting up Gradle Project

Create a Gradle project and add the below in the build.gradle dependencies,

dependencies {
    implementation group: 'org.springframework', name: 'spring-context', version: '5.3.22'
    implementation group: 'org.springframework', name: 'spring-core', version: '5.3.22'
    implementation group: 'org.springframework', name: 'spring-jdbc', version: '5.3.22'
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.30'

Based on what database you are using add the connector - in this example, we have used MySQL, you can get all these dependencies strings from

The Project Structure:

Spring JDBC Template Project Structure

Create the Database Table with some records:

Let's create a simple table student,

CREATE TABLE `student` (
  `student_id` int NOT NULL,
  `student_name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`student_id`));
insert into student values(1, 'Sam');

Create Properties file:

Create Application Config class:
package config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class AppConfig {

	Environment environment;

	DataSource dataSource() {
		DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
		return driverManagerDataSource;

Create the Student Pojo and Dao and Dao Impl:
package entities;

public class Student {

    private int studentId;
    private String studentName;

    public int getStudentId() {
        return studentId;

    public void setStudentId(int studentId) {
        this.studentId = studentId;

    public String getStudentName() {
        return studentName;

    public void setStudentName(String studentName) {
        this.studentName = studentName;

    public String toString() {
        return "Student{" +
                "studentId=" + studentId +
                ", studentName='" + studentName + '\'' +
package dao;

import entities.Student;

public interface StudentDao {
	Student getStudentById(int studentId);
package dao;

import javax.sql.DataSource;

import entities.Student;
import entities.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

public class StudentDaoImpl implements StudentDao {

	JdbcTemplate jdbcTemplate;

	public StudentDaoImpl(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);

	public Student getStudentById(int studentId) {
		return jdbcTemplate.queryForObject("select * from student where student_id = ?", new Object[] { studentId }, new StudentRowMapper());
package entities;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class StudentRowMapper implements RowMapper<Student> {

	public Student mapRow(ResultSet resultSet, int noOfRows) throws SQLException {

		Student student = new Student();
		return student;

Testing our Results
import config.AppConfig;
import dao.StudentDao;
import entities.Student;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringJdbcTemplateExample {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		StudentDao studentDao = context.getBean(StudentDao.class);
		Student student = studentDao.getStudentById(1);

Student{studentId=1, studentName='Sam'}

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap