Spring Boot JDBC Tutorial- Hello World example | JavaInUse

Spring Boot JDBC implementation- Hello World Example

In a previous post we had created an application using JDBC and seen the disadvantages. In another post we had seen the advantages of using Spring JDBC. In this chapter we see how to implement JDBC using Spring boot with MySql database.


This tutorial is explained in the below Youtube Video.

Why use Spring Boot JDBC-

The functionality of Spring JDBC and Spring Boot JDBC are the same. Only the implementation is made simple. The following are the advantages of Spring Boot JDBC over Spring JDBC.
JDBC using Spring JDBC using Spring Boot
Multiple dependencies like spring-context, spring-jdbc need to be specified. Only a single spring-boot starter dependency is required.
Necessary to create a database bean either using xml or javaconfig.

<bean id="dataSource"
<property name="driverClass" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:database.dat;shutdown=true" />
<property name="username" value="sa" />
<property name="password" value="" />
Datasource bean gets initialized automatically if not mentioned explicitly.
If user does not want this then it can be done by setting the property spring.datasource.initialize to false.
The Template beans PlatformTransactionManager, JdbcTemplate, NamedParameterJdbcTemplate must be registered If the Template beans PlatformTransactionManager, JdbcTemplate, NamedParameterJdbcTemplate not registered, then Spring Boot will register them automatically.
If any db initialization scripts like dropping or creation of tables are created in sql file. This info needs to be given explicitly in the configuration. Any db initialization scripts stored in schema-.sql gets executed automatically.

Lets Begin-

The project will be as follows-

Add the spring-jdbc-starter dependency.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


	<description>Demo project for Spring Boot</description>

		<relativePath /> <!-- lookup parent from repository -->






In the application.properties file specify the datasource properties

Create the schema-mysql.sql file and specify the initialization scripts-

CREATE TABLE employee (
  empName VARCHAR(100) NOT NULL

Create the Employee Domain class
package com.javainuse.model;

public class Employee {

	private String empId;
	private String empName;

	public String getEmpId() {
		return empId;

	public void setEmpId(String empId) {
		this.empId = empId;

	public String getEmpName() {
		return empName;

	public void setEmpName(String empName) {
		this.empName = empName;

	public String toString() {
		return "Employee [empId=" + empId + ", empName=" + empName + "]";


Create Service interface to specify employee operations to be performed.
package com.javainuse.service;

import java.util.List;

import com.javainuse.model.Employee;

public interface EmployeeService {
	void insertEmployee(Employee emp);
	void insertEmployees(List<Employee> employees);
	void getAllEmployees();
	void getEmployeeById(String empid);

The Service class implementation.
package com.javainuse.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

public class EmployeeServiceImpl implements EmployeeService {

	EmployeeDao employeeDao;

	public void insertEmployee(Employee employee) {

	public void insertEmployees(List<Employee> employees) {

	public void getAllEmployees() {
		List<Employee> employees = employeeDao.getAllEmployees();
		for (Employee employee : employees) {

	public void getEmployeeById(String empId) {
		Employee employee = employeeDao.getEmployeeById(empId);


Create the DAO interface.
package com.javainuse.dao;

import java.util.List;

import com.javainuse.model.Employee;

public interface EmployeeDao {
	void insertEmployee(Employee cus);
	void insertEmployees(List<Employee> employees);
	List<Employee> getAllEmployees();
	Employee getEmployeeById(String empId);

The DAO implementation class.
package com.javainuse.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.javainuse.dao.EmployeeDao;
import com.javainuse.model.Employee;

public class EmployeeDaoImpl extends JdbcDaoSupport implements EmployeeDao{
	DataSource dataSource;
	private void initialize(){
	public void insertEmployee(Employee emp) {
		String sql = "INSERT INTO employee " +
				"(empId, empName) VALUES (?, ?)" ;
		getJdbcTemplate().update(sql, new Object[]{
				emp.getEmpId(), emp.getEmpName()
	public void insertEmployees(List<Employee> employees) {
		String sql = "INSERT INTO employee " + "(empId, empName) VALUES (?, ?)";
		getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				Employee employee = employees.get(i);
				ps.setString(1, employee.getEmpId());
				ps.setString(2, employee.getEmpName());
			public int getBatchSize() {
				return employees.size();

	public List<Employee> getAllEmployees(){
		String sql = "SELECT * FROM employee";
		List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
		List<Employee> result = new ArrayList<Employee>();
		for(Map<String, Object> row:rows){
			Employee emp = new Employee();
		return result;

	public Employee getEmployeeById(String empId) {
		String sql = "SELECT * FROM employee WHERE empId = ?";
		return (Employee)getJdbcTemplate().queryForObject(sql, new Object[]{empId}, new RowMapper<Employee>(){
			public Employee mapRow(ResultSet rs, int rwNumber) throws SQLException {
				Employee emp = new Employee();
				return emp;

Finally create the class with @SpringBootApplication annotation.
package com.javainuse;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.impl.EmployeeServiceImpl;

public class SpringBootJdbcApplication {
	EmployeeService employeeService;

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(SpringBootJdbcApplication.class, args);
		EmployeeService employeeService = context.getBean(EmployeeService.class);
		Employee emp= new Employee();
		Employee emp1= new Employee();
		Employee emp2= new Employee();


		List<Employee> employees = new ArrayList<>();


Start the application-

Download Source Code

Download it -
Spring Boot + JDBC Hello World Example

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions