Spring + JdbcTemplate + JdbcDaoSupport examples
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.
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insert(Customer customer){
String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
Connection conn = null;
try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, customer.getCustId());
ps.setString(2, customer.getName());
ps.setInt(3, customer.getAge());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
2. Example With JdbcTemplate
With JdbcTemplate
, you save a lot of typing on the redundant codes, becuase JdbcTemplate
will handle it automatically.
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insert(Customer customer){
String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update(sql, new Object[] { customer.getCustId(),
customer.getName(),customer.getAge()
});
}
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.
public class JdbcCustomerDAO extends JdbcDaoSupport implements CustomerDAO
{
//no need to set datasource here
public void insert(Customer customer){
String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
getJdbcTemplate().update(sql, new Object[] { customer.getCustId(),
customer.getName(),customer.getAge()
});
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mkyongjava" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Note
In Spring JDBC development, it’s always recommended to useJdbcTemplate
andJdbcDaoSupport
, instead of coding JDBC code yourself.
最新文章
- SQL-truncate &;&; delete &;&; drop 的区别
- 【RabbitMQ】RabbitMQ在Windows的安装和简单的使用
- [AX2012]发送广播邮件
- 解决使用OCI连接oracle LNK2019: 无法解析的外部符号的问题
- MySQL 从库日志比主库多
- 入门视频采集与处理(BT656简介)
- Redis hash数据类型操作
- http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/
- Poj 1511 Invitation Cards(spfa)
- [Locked] One Edit Distance
- 教你爱上Blocks(闭包)
- 【剑指offer】调整数组顺序
- 浅谈IOS8之size class
- 第25篇 jQuer快速学习(上)---选择器和DOM操作
- MyEclipse出现红色感叹号解决办法
- UVA1619 栈维护递增序列
- Android必知必会-Android Studio修改包名
- DE1-SOC资源
- ABP框架用Dapper实现通过SQL访问数据库
- 【MongoDB-MongoVUE图像管理工具】
热门文章
- 自定义View(1)简单流程及示例模板
- 客户视角:Oracle ETL工具ODI
- Toad创建DBLINKsop
- UVa 1467 (贪心+暴力) Installations
- javaScript的函数(Function)对象的声明(@包括函数声明和函数表达式)
- handler.post 为什么要将thread对象post到handler中执行呢?
- Java 单元测试(Junit)
- addView的误区
- ubuntu鼠标突然不能使用的解决方法
- hdu 1573 x问题(中国剩余定理)HDU 2007-1 Programming Contest