Easy learning with example program codes

spring jdbc tutorial

Problems with JDBC API:

We have to write unnecessary code for creating database connections, SQL statement executions, exception handling, transaction handling and closing database connections etc.

Spring JDBC Framework:

In case of Spring JDBC Framework it takes care of all things like creating database connections, executing SQL statements, handling exceptions, handling transactions and closing database connections etc.

Spring JDBC Approaches:

  • JdbcTemplate
  • NamedParameterJdbcTemplate
  • SimpleJdbcTemplate
  • SimpleJdbcInsert
  • SimpleJdbcCall

Note: The most popular approach uses JdbcTemplate class.

JdbcTemplate Class:

JdbcTemplate is the central class in the JDBC core package. It takes care of creating and closing the database connections. It executes the sql queries, updates and store procedures etc. It handles JDBC exceptions and translating them to the generic message.

Commonly user methods of JdbcTemplate class:

  • public int update(String query): Issue a single SQL update operation (such as an insert, update or delete statement).
  • public int update(String query,Object… args): Issue a single SQL update operation (such as an insert, update or delete statement) via a prepared statement, binding the given arguments.
  • public void execute(String query): Issue a single SQL execute, typically a DDL statement.
  • public T execute(String sql, PreparedStatementCallback action): Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement.
  • public T query(String sql, ResultSetExtractor rse): Execute a query given static SQL, reading the ResultSet with a ResultSetExtractor.
  • public List query(String sql, RowMapper rse): Execute a query given static SQL, mapping each row to a Java object via a RowMapper.

Note: JdbcTemplate class objects are threadsafe.

We are using Spring boot here.


package com.codesjava.SpringDB;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
public class SpringDbApplication {
    private static ApplicationContext applicationContext;
    public static void main( String[] args ) {
    	applicationContext =, args);  
    private static void performDBOperation(){
    	JDBCController jdbcController = (JDBCController) applicationContext.getBean("jDBCController");
package com.codesjava.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
public class JDBCController {
    JdbcTemplate jdbc;  
    public void insertData(){  
        jdbc.execute("insert into users(seq,user_name, password)values(12,'hkumar', 'h123')");  
        System.out.println("Data inserted Successfully");  


Data inserted Successfully

Related topics

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki