首先,数据库是这样的,很简单。
当然,要引入spring的包,这里我全部导入了,省事。
applicationContext.xml是这样的:
- <?xml version="1.0" encoding="UTF-8"?>
- <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="test" class="jdbc.Test">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <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/testspring" />
- <property name="username" value="root" />
- <property name="password" value="" />
- </bean>
- </beans>
User.java是这样的:
- package jdbc;
- public class User {
- private int id;
- private String name;
- private String password;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
下面来对比一下三种写法:
1、spring
- package jdbc;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test {
- private DataSource dataSource;
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- public void insert(User u) {
- String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setString(1, u.getName());
- ps.setString(2, u.getPassword());
- ps.executeUpdate();
- ps.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- }
- }
- }
- }
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- Test t = (Test) ctx.getBean("test");
- User u = new User();
- u.setName("dd");
- u.setPassword("dd");
- t.insert(u);
- }
- }
运行结果是这样的:
2、JdbcTemplate
- package jdbc;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jdbc.core.JdbcTemplate;
- public class Test {
- private DataSource dataSource;
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- public void insert(User u) {
- String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
- JdbcTemplate template = new JdbcTemplate(dataSource);
- template.update(sql, new Object[]{u.getName(), u.getPassword()});
- }
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- Test t = (Test) ctx.getBean("test");
- User u = new User();
- u.setName("dd");
- u.setPassword("dd");
- t.insert(u);
- }
- }
可以看得出简单了很多,不用Connection了。
3、JdbcDaoSupport
这个更简单,不用new JdbcTemplate了。
- package jdbc;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
- public class Test extends JdbcDaoSupport {
- //JdbcDaoSupport类已经有了public final void setDataSource(DataSource dataSource)了
- //不用重写也不能重写
- public void insert(User u) {
- String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句
- this.getJdbcTemplate().update(sql, new Object[]{u.getName(), u.getPassword()});
- }
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- Test t = (Test) ctx.getBean("test");
- User u = new User();
- u.setName("dd");
- u.setPassword("dd");
- t.insert(u);
- }
- }
三种方法哪一种更简单一目了然。
相关推荐
工程实现的是Spring+SpringMVC+JDBCTemplate简单用户管理功能,界面十分粗糙,只是演示功能。
基于java+Spring+SpringMVC+JDBCTemplate+JSP开发的博客论坛系统+源码+开发文档+视频演示,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于java+Spring+...
Spring Security 3.1 +Spring +Servlet+JdbcTemplate 自己找的资料,并开发成功
struts+spring +jdbctemplate demo http://knight-black-bob.iteye.com/blog/2262258
struts2+hibernate+spring+jdbctemplate+EXT集成实例
使用strut2+spring+jdbctemplate+ajax(jquery,ajaxanywhere)去实现的一个简易用户管理,和留言功能
Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA+JdbcTemplate一个简单的DEMO
管理系统系列--图书管理系统( Spring+Spring MVC+JdbcTemplate)
这个本人经常精心调试出来的示例,import 进来就可以使用,例子简单,附有数据库脚本 Struts2+Spring+EasyUI+JDBCTemplate学习源码
简单的一个demo,只是实现了框架以及spring3.1.1原生支持的Cache不需要第三方插件
strut2+spring+springjdbctemplate做的简易登录系统
spring-jdbcTemplate实例工程
前期项目 Spring+Spring MVC+JDBC Template 增删改查,包含建库,前端加后台
人工智能-毕业设计-管理信息系统-基于java的图书管理系统( Spring+Spring MVC+JdbcTemplate) 基于Spring+Spring MVC(Maven方式构建) Build Status Hex.pm 项目简介 本图书管理系统基于spring,spring mvc,数据库为...
1.针对SpringMVC注解的配置...2.Spring+JdbcTemplate事物管理 3.Spring+Hibernate事物管理 4.Spring实现DWR注解方式的应用配置 5.Spring配置拦截器 6.Spring通过拦截器实现防止重复提交实例 对学习,会起到非常好的效果
spring mvc+jdbctemplate非注解例子包含aop 连接池等.使用的jar包需要spring2.5以上
基于Spring+Spring MVC(Maven方式构建) 项目简介 本图书管理系统基于spring,spring mvc,数据库为mysql。前端使用了Bootstrap。 系统功能 该系统实现读者和管理员登陆,图书的增删改查,读者的增删改查,借还图书,...
采用springmvc+jdbctemplate+mysql注解方式搭建,中小型项目首选!包含mysql数据库.
本资源包含了两个子项目,分别进行了springboot+jpa+jdbcTemplate的多数据源独立事务配置和jta分布式事务配置,并针对不同的情况编写了事务配置测试接口,还演示了JPA的domain一对多自动生成数据库表且不生成数据库...
#### 基于Spring+Spring MVC(Maven方式构建) [ ### 项目简介 本图书管理系统基于spring,spring mvc,数据库为mysql。前端使用了Bootstrap。 ### 系统功能 该系统实现读者和管理员登陆,图书的增删改查,读者的增删...