首先,数据库是这样的,很简单。
当然,要引入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 + JdbcTemplate + JdbcDaoSupport Examples