首先,数据库是这样的,很简单。

当然,要引入spring的包,这里我全部导入了,省事。

applicationContext.xml是这样的:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="test" class="jdbc.Test">
  7. <property name="dataSource" ref="dataSource"></property>
  8. </bean>
  9. <bean id="dataSource"
  10. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  11. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  12. <property name="url" value="jdbc:mysql://localhost:3306/testspring" />
  13. <property name="username" value="root" />
  14. <property name="password" value="" />
  15. </bean>
  16. </beans>

User.Java是这样的:

  1. package jdbc;
  2. public class User {
  3. private int id;
  4. private String name;
  5. private String password;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getPassword() {
  19. return password;
  20. }
  21. public void setPassword(String password) {
  22. this.password = password;
  23. }
  24. }

下面来对比一下三种写法:

1、spring

  1. package jdbc;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import javax.sql.DataSource;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. public class Test {
  9. private DataSource dataSource;
  10. public void setDataSource(DataSource dataSource) {
  11. this.dataSource = dataSource;
  12. }
  13. public void insert(User u) {
  14. String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
  15. Connection conn = null;
  16. try {
  17. conn = dataSource.getConnection();
  18. PreparedStatement ps = conn.prepareStatement(sql);
  19. ps.setString(1, u.getName());
  20. ps.setString(2, u.getPassword());
  21. ps.executeUpdate();
  22. ps.close();
  23. } catch (SQLException e) {
  24. throw new RuntimeException(e);
  25. } finally {
  26. if (conn != null) {
  27. try {
  28. conn.close();
  29. } catch (SQLException e) {
  30. }
  31. }
  32. }
  33. }
  34. public static void main(String[] args) {
  35. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  36. "applicationContext.xml");
  37. Test t = (Test) ctx.getBean("test");
  38. User u = new User();
  39. u.setName("dd");
  40. u.setPassword("dd");
  41. t.insert(u);
  42. }
  43. }

运行结果是这样的:

2、JdbcTemplate

  1. package jdbc;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import javax.sql.DataSource;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.jdbc.core.JdbcTemplate;
  9. public class Test {
  10. private DataSource dataSource;
  11. public void setDataSource(DataSource dataSource) {
  12. this.dataSource = dataSource;
  13. }
  14. public void insert(User u) {
  15. String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
  16. JdbcTemplate template = new JdbcTemplate(dataSource);
  17. template.update(sql, new Object[]{u.getName(), u.getPassword()});
  18. }
  19. public static void main(String[] args) {
  20. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  21. "applicationContext.xml");
  22. Test t = (Test) ctx.getBean("test");
  23. User u = new User();
  24. u.setName("dd");
  25. u.setPassword("dd");
  26. t.insert(u);
  27. }
  28. }

可以看得出简单了很多,不用Connection了。

3、JdbcDaoSupport

这个更简单,不用new JdbcTemplate了。

  1. package jdbc;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  5. public class Test extends JdbcDaoSupport {
  6. //JdbcDaoSupport类已经有了public final void setDataSource(DataSource dataSource)了
  7. //不用重写也不能重写
  8. public void insert(User u) {
  9. String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
  10. this.getJdbcTemplate().update(sql, new Object[]{u.getName(), u.getPassword()});
  11. }
  12. public static void main(String[] args) {
  13. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  14. "applicationContext.xml");
  15. Test t = (Test) ctx.getBean("test");
  16. User u = new User();
  17. u.setName("dd");
  18. u.setPassword("dd");
  19. t.insert(u);
  20. }
  21. }

Spring JdbcTemplate+JdbcDaoSupport实例(和比较)的更多相关文章

  1. ref:Spring JdbcTemplate+JdbcDaoSupport实例

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

  2. Spring JdbcTemplate+JdbcDaoSupport实例

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

  3. Spring + JdbcTemplate + JdbcDaoSupport examples

    In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the over ...

  4. Spring JdbcTemplate batchUpdate() 实例

    在某些情况下,可能需要将一批记录插入到数据库中.如果你对每条记录调用一个插件的方法,SQL语句将被重复编译,造成系统缓慢进行. 在上述情况下,你可以使用 JdbcTemplate BATCHUPDAT ...

  5. Spring JdbcTemplate查询实例

    这里有几个例子向您展示如何使用JdbcTemplate的query()方法来查询或从数据库提取数据.整个项目的目录结构如下: 1.查询单行数据 这里有两种方法来查询或从数据库中提取单行记录,并将其转换 ...

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

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

  7. Spring JdbcTemplate 的使用与学习(转)

    紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...

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

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

  9. JAVA入门[18]-JdbcTemplate简单实例

    一.关于JdbcTemplate JdbcTemplate是最基本的Spring JDBC模板,这个模板支持简单的JDBC数据库访问功能以及基于索引参数的查询. Spring数据访问模板:在数据库操作 ...

随机推荐

  1. 剑指offer——和为s的两个数字VS和为s的连续正数序列

    两种方法都类似于快排的变形. #include <iostream> #include <string> using namespace std; bool FindNumbe ...

  2. CDH- 集群时间同步ntp问题解决

    在CDH集群中发现有两台机器获取不到心跳(),导致监控不了机器状态,出现告警 可以使用ntpstat检查与ntp 服务器的时间偏差状态 使用 ntpstat 发现没有同步到ntp时间服务器,运行 nt ...

  3. artDialog 简单使用!

    简介 artDialog是一个轻巧且高度兼容的javascript对话框组件,可让你的网页交互拥有桌面软件般的用户体验. 功能: 支持锁定屏幕(遮罩).模拟alert和confirm.多窗口弹出.静止 ...

  4. Centos7部署NFS

    server1:192.168.1.189   ###客户端 server2:192.168.1.190    ##服务端 1.首先创建共享目录. mkdir -p /data/share 安装nfs ...

  5. Centos7搭建Mysql-5.6.38,及主从复制。

    Server1:192.168.1.189  (主) Server2:192.168.1.190  (从) 1.关闭默认的firewalld防火墙,安装iptables. systemctl disa ...

  6. Hibernate - 配置c3p0

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuratio ...

  7. 1147. Heaps (30)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  8. 删除文件时提示 你需要来自system的权限才能对此文件夹进行更改

    问题描述: 我的计算机是Win7 x64操作系统,在我的计算机的F盘中,不知道什么时候多了个“12e4k69m762nzcgt8zx”这样一个文件夹,应该是某个软件自己创建并留下的文件夹,想删除掉则提 ...

  9. pip3 更改安装源

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  10. WCF中WcfSvcHost.exe如何禁止自动启动

    今天同事问在一个WCF server的解决方案里调试时如何禁止Server自动启动. 经过调查发现, VS的工具WcfSvcHost会在调试时自动扫描工程里的WCF server, 然后启动起来. 如 ...