In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the overall database operation processes.

In this tutorial, we will reuse the last Spring + JDBC example, to see the different between a before (No JdbcTemplate support) and after (With JdbcTemplate support) example.

1. Example Without JdbcTemplate

Witout JdbcTemplate, you have to create many redundant codes (create connection , close connection , handle exception) in all the DAO database operation methods – insert, update and delete. It just not efficient, ugly, error prone and tedious.

  1. private DataSource dataSource;
  2. public void setDataSource(DataSource dataSource) {
  3. this.dataSource = dataSource;
  4. }
  5. public void insert(Customer customer){
  6. String sql = "INSERT INTO CUSTOMER " +
  7. "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
  8. Connection conn = null;
  9. try {
  10. conn = dataSource.getConnection();
  11. PreparedStatement ps = conn.prepareStatement(sql);
  12. ps.setInt(1, customer.getCustId());
  13. ps.setString(2, customer.getName());
  14. ps.setInt(3, customer.getAge());
  15. ps.executeUpdate();
  16. ps.close();
  17. } catch (SQLException e) {
  18. throw new RuntimeException(e);
  19. } finally {
  20. if (conn != null) {
  21. try {
  22. conn.close();
  23. } catch (SQLException e) {}
  24. }
  25. }
  26. }

2. Example With JdbcTemplate

With JdbcTemplate, you save a lot of typing on the redundant codes, becuase JdbcTemplate will handle it automatically.

  1. private DataSource dataSource;
  2. private JdbcTemplate jdbcTemplate;
  3. public void setDataSource(DataSource dataSource) {
  4. this.dataSource = dataSource;
  5. }
  6. public void insert(Customer customer){
  7. String sql = "INSERT INTO CUSTOMER " +
  8. "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
  9. jdbcTemplate = new JdbcTemplate(dataSource);
  10. jdbcTemplate.update(sql, new Object[] { customer.getCustId(),
  11. customer.getName(),customer.getAge()
  12. });
  13. }

See the different?

3. Example With JdbcDaoSupport

By extended the JdbcDaoSupport, set the datasource and JdbcTemplate in your class is no longer required, you just need to inject the correct datasource into JdbcCustomerDAO. And you can get the JdbcTemplate by using a getJdbcTemplate() method.

  1. public class JdbcCustomerDAO extends JdbcDaoSupport implements CustomerDAO
  2. {
  3. //no need to set datasource here
  4. public void insert(Customer customer){
  5. String sql = "INSERT INTO CUSTOMER " +
  6. "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
  7. getJdbcTemplate().update(sql, new Object[] { customer.getCustId(),
  8. customer.getName(),customer.getAge()
  9. });
  10. }
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="dataSource"
  6. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  7. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  8. <property name="url" value="jdbc:mysql://localhost:3306/mkyongjava" />
  9. <property name="username" value="root" />
  10. <property name="password" value="password" />
  11. </bean>
  12. </beans>
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">
  6. <property name="dataSource" ref="dataSource" />
  7. </bean>
  8. </beans>

Note

In Spring JDBC development, it’s always recommended to use JdbcTemplate and JdbcDaoSupport, instead of coding JDBC code yourself.

Spring + JdbcTemplate + JdbcDaoSupport examples的更多相关文章

  1. Spring JdbcTemplate Querying examples

    Here are few examples to show you how to use JdbcTemplate query() methods to query or extract data f ...

  2. ref:Spring JdbcTemplate+JdbcDaoSupport实例

    ref:https://www.yiibai.com/spring/spring-jdbctemplate-jdbcdaosupport-examples.html 在Spring JDBC开发中,可 ...

  3. Spring JdbcTemplate+JdbcDaoSupport实例

    在Spring JDBC开发中,可以使用 JdbcTemplate 和 JdbcDaoSupport 类来简化整个数据库的操作过程. 在本教程中,我们将重复上一篇文章Spring+JDBC例子,看之前 ...

  4. Spring JdbcTemplate+JdbcDaoSupport实例(和比较)

    首先,数据库是这样的,很简单. 当然,要引入spring的包,这里我全部导入了,省事. applicationContext.xml是这样的: <?xml version="1.0&q ...

  5. 使用Spring JDBCTemplate简化JDBC的操作

    使用Spring JDBCTemplate简化JDBC的操作 接触过JAVA WEB开发的朋友肯定都知道Hibernate框架,虽然不否定它的强大之处,但个人对它一直无感,总感觉不够灵活,太过臃肿了. ...

  6. Spring——JdbcTemplate

    一.JdbcTemplate介绍: 为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架,Spring Boot Spring Data ...

  7. 【Spring】Spring JdbcTemplate

    Spring JdbcTemplate 文章源码 JdbcTemplate 概述 它是 Spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装.Spring 框架提供了很多的操 ...

  8. 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析

    Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...

  9. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

随机推荐

  1. 一些非常有用的html,css,javascript代码片段(持久更新)

    1.判断设备是否联网 if (navigator.onLine) { //some code }else{ //others code } 2.获取url的指定参数 function getStrin ...

  2. Java中静态代码块,代码块,构造方法优先级、区别及代码示例

    在项目中遇到了代码块的知识点,跑了下测试,写下结论 代码优先级:静态代码块 -> 构造代码块 -> 构造方法 多个代码块优先级,按照“先定义的代码先执行,后定义的代码后执行”原则执行 静态 ...

  3. Android开发之EventBus的简单使用

    参考: 1.http://blog.csdn.net/harvic880925/article/details/40660137 2.http://blog.csdn.net/harvic880925 ...

  4. 如何在Ubuntu上安装最新版本的Node.js

    apt-get update apt-get install -y python-software-properties software-properties-common add-apt-repo ...

  5. struts2 获取前台表单的值?? 原理??

    struts2中,在ACTION中申明一个变量 private string 变量名:然后设置变量名 的get/set方法: 在运行的时候struts2会自动获取. 比如:jsp 页面中有个文本框&l ...

  6. css配合js模拟的select下拉框

    css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...

  7. Redis必要的一些配置

    [root@localhost202 redis-2.8.19]# /usr/local/redis/bin/redis-server >> /data/redis-start.txt   ...

  8. LeetCode Single Number II 单元素2

    题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这 ...

  9. 解决jQuery对表单serialize后出现的乱码问题

    通过看jQuery源码可以知道,serialize方法是通过encodeURIComponent编码的,所以解决乱码的最笨方法:  1.重新分解序列化后的值  2.把分解的值重新decodeURICo ...

  10. fmri当前相关软件工具整理

    1.spm; 2.afni; 3.fsl; 4.drtools; 5.prtools; 6.phycaa+; 7.cca-fmri;