Select For Update Spring Jdbctemplate



  1. Spring Jdbctemplate Query
  2. Select For Update Spring Jdbctemplate Printable
  3. Jdbctemplate Run Update Query
  4. Select For Update Spring Jdbctemplate 2020

In this Spring CRUD Example, we will build a Simple Spring Application and perform CRUD operations using Spring JdbcTemplate. We will create a simple Employee management application which has abilities to create a new employee, update the existing employee, get a particular employee/ all employee and finally delete the existing employee.

Creating table

Spring JdbcTemplate: Performing insert, update, delete, select operations February 15, 2016 0 This article explains JdbcTemplate in Spring and using it to perform CRUD operations. Spring 4 JdbcTemplate Annotation Example. By Yashwant Chavan, Views 558927, Last updated on 15-Dec-2016. In this tutorial, we will learn how to connect to the database and execute CRUD SQL queries using Spring 4 JdbcTemplate. The JdbcTemplate class executes SQL queries, update statements and stored procedure calls, performs iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.

Create EMPLOYEETable, simply Copy and Paste the following SQL query in the query editor to get the table created.

Folder Structure:

  1. Create a simple Maven Project “SpringJDBC”by selecting maven-archetype-quickstart and create a package for our source files com.javainterviewpointunder src/main/java
  2. Now add the following dependency in the POM.xml
  3. Create the Java classes Employee.java,EmployeeDAOImpl.java andSpringJDBCExample.java under com.javainterviewpointfolder.

Other interesting articles which you may like …

Spring CRUD Example

Employee.java

Our Employee class is a simple POJO class consisting getters and setters of Employee properties id, name, age, dept.

SpringConfig.xml

In our configuration file, we have defined the three beans

  1. DriverManagerDataSourceDriverManagerDataSource contains database related configurations such as driver class name, connection URL, username and password.
  2. JdbcTemplate – We will be referencing the dataSource id (DriverManagerDataSource ) to the property dataSource of the JdbcTemplate class.
  3. EmployeeDAOImpl – We will be referencing the jdbcTemplate id to the property jdbcTemplate of the EmployeeDAOImpl class.

EmployeeDAO.java

EmployeeDAOImpl.java

EmployeeDAOImpl class implements the interface EmployeeDAO and overrides all the unimplemented methods. We have the below methods in our EmployeeDAOImpl class

  • setJdbcTemplate() – Through Spring setter injection we will be injecting the jdbcTemplate from the Spring configuration file.
  • getAllEmployee() – In order to fetch all the records from the database we just need to pass the SQL and the instance of the ResultSetExtractor to the query() method of jdbcTemplate. ResultSetExtractor interface accepts the ResultSet and returns a Java List. We need to override the extractData() method and map each ResultSet to an Employee object add to a list.
  • getEmployeeById() – In order to fetch a particular record we just need to pass the SQL and the instance of the RowMapper to the queryForObject() method of jdbcTemplate. RowMapper interface internally iterates the ResultSet and adds it to the Collection (Map). Hence there is no need for us to iterate the ResultSet as we do in the ResultSetExtractor.
  • updateEmployee() – We will be updating the corresponding employee by calling the update() method of the jdbcTemplate passing the SQL and the parameters.
  • deleteEmployee() – In order to delete an employee, we need to call the update() method of the jdbcTemplate passing the SQL and the id.

SpringJDBC.java

  • ClassPathXmlApplicationContext class reads our Configuration File(SpringConfig.xml)
  • We will get our EmployeeDAOImpl Class instance by calling the getBean() method over the context.
  • Call the saveEmployee(), getEmployeeById(), getAllEmployees(),updateEmployee() and deleteEmployee() methods over the EmployeeDAOImpl instance which we got above.

Output:

Download Source Code

By Yashwant Chavan, Views 558927, Last updated on 15-Dec-2016

In this tutorial, we will learn how to connect to the database and execute CRUD SQL queries using Spring 4 JdbcTemplate. Java base configuration is used to load the JdbcTemplate, here we are not using XML base configuration for JdbcTemplate.

tags spring

Spring 4 Eclipse project set up

Tools and Technologies

  1. Apache Maven 3.0.4
  2. JDK 1.8
  3. Spring core, Spring webmvc and Spring context (4.1.4.RELEASE)
  4. mysql(5.1.31)

Database table

Use below SQL script to create 'trn_person' table in the database.

Spring Jdbctemplate Query

pom.xml

As we are using Maven project. Let's define the spring 4 specific maven dependencies.

Person Pojo

This is simple Person pojo class which contains different attributes like personId, firstName, lastName and age.

Select For Update Spring Jdbctemplate Printable

application.properties

Create 'application.properties' file under /resources folder. Define data source configuration properties like jdbc driverClassName, url, username and password.

Spring 4 Application Configuration

Select for update spring jdbctemplate training

@Configuration annotation imports the Spring configuration. @Configuration objects are managed as Spring beans within the container, imported configurations are used to injected using @Autowired or @Inject.

Sql

@ComponentScan is equivalent to <context:component-scan base-package='...' used to lookup the beans and components classes in the spring context.

@PropertySource Annotation use to provide a convenient and declarative mechanism for adding a PropertySource to Spring's Environment.

To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory. Here we have registered JdbcTemplate and DataSource beans.

PersonService Interface

Creates PersonService interface along with CRUD methods.

  1. Create new Person entity.
  2. Edit Person entity.
  3. Delete Person entity.
  4. Returns Person entity identified by the given id.
  5. Returns all Person entities

PersonService Implementation

Mark PersonServiceImpl class as 'personService' using @Service annotation. Use @Autowired annotation to autowire PersonDao bean.

PersonDao Interface

Creates PersonDao interface.

PersonDao implementation

PersonDaoImpl marked with @Repository annotation, It allows the component scanning to find and configure the respected DAO. @Autowired JdbcTemplate to access to a persistence resource.

App.java

To create standalone Spring Application Context, We are using AnnotationConfigApplicationContext which helps to register all the beans generated by the configuration class (@Configuration) at Spring runtime.

Use AbstractApplicationContext.getBean(String name) method is used to get bean object( 'personService') from Spring application context.

Once you get the 'personService' instance perform different CRUD operations.

Spring

Output

Marcus

Help me lot, thanks for step by step guide.

Is anyway I can download this spring jdbc source code ?

Thanks
Marcus

Kavitha Murthi

Oracle sql select for update

Good work, please share post on Spring boot & Micro Services.

Thanks

Surendra Kumar

Nice post !!!

Jdbctemplate

Oksana

Thank you! Very helpful

Jdbctemplate Run Update Query

Nitin Jain

Thanks for posting Spring 4 jdbctemplate CRUD example!!!

Select For Update Spring Jdbctemplate 2020

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]