This tutorial show you how to connect database using Spring JdbcTemplate. It is a important part of the Srping JDBC. It takes care of managing JDBC resources such as creating and closing of database connection. It also handles the exception and provides and returns the informative messages. Now you can build a application to connect database using Spring JdbcTemplate. It performs the operations with the database by the support of JdbcTemplate class such as insertion, updation, deletion and retrieval of the data from your database.
Here, this tutorial uses the Oracle11g database for creating the following table.
1 2 3 4 5 |
create table FRUIT( id number(10), name varchar2(100), produceby varchar2(100) ); |
The spring application is built by three steps:
1) Create the class Fruit.java
This class is implemented such as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.javabycode.spring; public class Fruit { private Integer id; private String name; private String produceby; Fruit(Integer id, String name, String produceby){ this.id = id; this.name = name; this.produceby = produceby; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setProduceby(String produceby) { this.produceby = produceby; } public String getProduceby() { return produceby; } } |
2) Create the class FruitDao.java
This class has one property jdbcTemplate and three methods. The implementation of class such as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javabycode.spring; import org.springframework.jdbc.core.JdbcTemplate; public class FruitDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public int saveFruit(Fruit e) { String query = "insert into fruit values('" + e.getId() + "','" + e.getName() + "','" + e.getProduceby() + "')"; return jdbcTemplate.update(query); } public int updateFruit(Fruit e) { String query = "update fruit set name='" + e.getName() + "',salary='" + e.getProduceby() + "' where id='" + e.getId() + "' "; return jdbcTemplate.update(query); } public int deleteFruit(Fruit e) { String query = "delete from fruit where id='" + e.getId() + "' "; return jdbcTemplate.update(query); } } |
3) Create the spring configuration applicationContext.xml
To do with the database, you should configure spring context with three beans such as below:
Firstly, the bean DriverManagerDataSource is defined with the information about the database such as driver class name, connnection URL, username and password.
Secondly, the bean JdbcTemplate is defined to refer to the bean DriverManagerDataSource
Third, the bean FruitDao is defined to refer to the bean JdbcTemplate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="managerDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:demo" /> <property name="username" value="javabycode" /> <property name="password" value="democonora" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="managerDS"></property> </bean> <bean id="fruitDao" class="com.javabycode.spring.FruitDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans> </beans> |
You should implement the class main with name SpringJdbcTemplateSample to do the oprations using Jdbc Template. Firstly, you get the FruitDao bean from spring context then call the saveFruit() method, and you could uncomment to run two methods updateFruit() and deleteFruit() as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.javabycode.spring; package com.javabycode.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringJdbcTemplateSample { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); FruitDao dao = (FruitDao) ctx.getBean("fruitDao"); int status = dao.saveFruit(new Fruit(88, "Orange", "Cuba")); System.out.println(status); /* * int status=dao.updateFruit(new Fruit(88,"Orange","India")); * System.out.println(status); */ /* * Fruit fruit=new Fruit(); * fruit.setId(102); * int status=dao.deleteFruit(e); * System.out.println(status); */ } } |
Finally, this tutorial show you the completely example how to connect database using Spring JdbcTemplate. If you have any opinion or questions about it, please leave a comment.
Happy learning!
Thіs design is wiϲked! You definitely know how to keеp
a reader entertained. Betաeen your wit and your videοs, I ᴡas aⅼmost moveɗ to
start my own blog (well, almost…HaHa!) Wоndeгful jоb.
I really loᴠed what уou had to say, and more than that,
hoѡ you presented іt. Too cooⅼ!
Thank for your comment