Spring + JdbcTemplate + JdbcDaoSupport examples
2015-06-09 13:35
621 查看
In Spring JDBC development, you can use
JdbcTemplateand
JdbcDaoSupportclasses 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 use
JdbcTemplateand
JdbcDaoSupport, instead of coding JDBC code yourself.
Download Source Code
Download it – Spring-JDBC-Example.zip (15 KB)相关文章推荐
- java web汉字传参的解决办法
- java 桌面面板与内部窗体用法
- 如何将eclipse项目和svn关联(从服务器取项目)
- myeclipse 复制类名
- Spring + JDBC example
- java中异步调用的解决方法
- Android NDK (学习笔记七) —— JNI的交互处理之Eclipse开发工具配置
- scala,maven,eclipse 项目构建
- hdoj 1007 java MLE通不过
- java.lang.ClassNotFoundException: org.json.JSONException一种解决方案
- Java-反转单链表
- java 线程池ThreadPoolExecutor 如何与 AsyncTask() 组合使用。
- java -cp
- 生产环境下JAVA进程高CPU占用故障排查
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- JAVASE学习笔记:第十章 SWing常用控件类(一)
- JAVASE学习笔记:第九章 JAVA异常类
- [置顶] Java断点续传下载视频
- JAVASE学习笔记:第八章 常用类Util工具包之日期类、数字类
- Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务