Spring Jdbc Approaches
Spring framework provides following approaches for JDBC database access:
o JdbcTemplate
o NamedParameterJdbcTemplate
o SimpleJdbcTemplate
o SimpleJdbcInsert and SimpleJdbcCall
JdbcTemplate class
It is the central class in the Spring JDBC support classes. It takes care of creation and release of resources such as
creating and closing of connection object etc. So it will not lead to any problem if you forget to close the connection.
It handles the exception and provides the informative exception messages by the help of excepion classes defined in
the org.springframework.dao package.
We can perform all the database operations by the help of JdbcTemplate class such as insertion, updation, deletion and
retrieval of the data from the database.
Let's see the methods of spring JdbcTemplate class.
No. Method Description
1) public int update(String query) is used to insert, update and delete records.
2) public int update(String query,Object... args) is used to insert, update and delete records using
PreparedStatement using given arguments.
3) public void execute(String query) is used to execute DDL query.
4) public T execute(String sql, executes the query by using PreparedStatement
PreparedStatementCallbackaction) callback.
5) public T query(String sql, is used to fetch records using ResultSetExtractor.
ResultSetExtractor rse)
6) public List query(String sql, RowMapper rse) is used to fetch records using RowMapper.
Example of Spring JdbcTemplate
We are assuming that you have created the following table inside the Oracle10g database.
1. create table employee(
2. id number(10),
3. name varchar2(100),
4. salary number(10)
5. );
Employee.java
This class contains 3 properties with constructors and setter and getters.
1. package com.javatpoint;
2.
3. public class Employee {
4. private int id;
5. private String name;
6. private float salary;
7. //no-arg and parameterized constructors
8. //getters and setters
9. }
EmployeeDao.java
It contains one property jdbcTemplate and three methods saveEmployee(), updateEmployee and deleteEmployee().
1. package com.javatpoint;
2. import org.springframework.jdbc.core.JdbcTemplate;
3.
4. public class EmployeeDao {
5. private JdbcTemplate jdbcTemplate;
6.
7. public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
8. this.jdbcTemplate = jdbcTemplate;
9. }
10.
11. public int saveEmployee(Employee e){
12. String query="insert into employee values(
13. '"+e.getId()+"','"+e.getName()+"','"+e.getSalary()+"')";
14. return jdbcTemplate.update(query);
15. }
16. public int updateEmployee(Employee e){
17. String query="update employee set
18. name='"+e.getName()+"',salary='"+e.getSalary()+"' where id='"+e.getId()+"' ";
19. return jdbcTemplate.update(query);
20. }
21. public int deleteEmployee(Employee e){
22. String query="delete from employee where id='"+e.getId()+"' ";
23. return jdbcTemplate.update(query);
24. }
25.
26. }
applicationContext.xml
The DriverManagerDataSource is used to contain the information about the database such as driver class name,
connnection URL, username and password.
There are a property named datasource in the JdbcTemplate class of DriverManagerDataSource type. So, we need to
provide the reference of DriverManagerDataSource object in the JdbcTemplate class for the datasource property.
Here, we are using the JdbcTemplate object in the EmployeeDao class, so we are passing it by the setter method but
you can use constructor also.
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans
7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
8.
9. <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
10. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
11. <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
12. <property name="username" value="system" />
13. <property name="password" value="oracle" />
14. </bean>
15.
16. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
17. <property name="dataSource" ref="ds"></property>
18. </bean>
19.
20. <bean id="edao" class="com.javatpoint.EmployeeDao">
21. <property name="jdbcTemplate" ref="jdbcTemplate"></property>
22. </bean>
23.
24. </beans>
Test.java
This class gets the bean from the applicationContext.xml file and calls the saveEmployee() method. You can also call
updateEmployee() and deleteEmployee() method by uncommenting the code as well.
1. package com.javatpoint;
2.
3. import org.springframework.context.ApplicationContext;
4. import org.springframework.context.support.ClassPathXmlApplicationContext;
5. public class Test {
6.
7. public static void main(String[] args) {
8. ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
9.
10. EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
11. int status=dao.saveEmployee(new Employee(102,"Amit",35000));
12. System.out.println(status);
13.
14. /*int status=dao.updateEmployee(new Employee(102,"Sonoo",15000));
15. System.out.println(status);
16. */
17.
18. /*Employee e=new Employee();
19. e.setId(102);
20. int status=dao.deleteEmployee(e);
21. System.out.println(status);*/
22.
23. }
24.
25. }