首先,數據庫是這樣的,很簡單。 html
固然,要引入spring的包,這裏我所有導入了,省事。 java
applicationContext.xml是這樣的: mysql
- < span>xmlversion="1.0"encoding="UTF-8"?<
- < span>beansxmlns="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"<
-
- < span>beanid="test"class="jdbc.Test"<
- < span>propertyname="dataSource"ref="dataSource"<</>property<
- </>bean<
-
- < span>beanid="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource"<
- < span>propertyname="driverClassName"value="com.mysql.jdbc.Driver"/<
- < span>propertyname="url"value="jdbc:mysql://localhost:3306/testspring"/<
- < span>propertyname="username"value="root"/<
- < span>propertyname="password"value=""/<
- </>bean<
-
- </>beans<
User.java是這樣的: spring
- package jdbc;
-
- publicclass User {
- privateint id;
- private String name;
- private String password;
-
- publicint getId() {
- return id;
- }
- publicvoid setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- publicvoid setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- publicvoid setPassword(String password) {
- this.password = password;
- }
-
- }
下面來對比一下三種寫法:
一、spring sql
- 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;
-
- publicclass Test {
- private DataSource dataSource;
-
- publicvoid setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
-
- publicvoid 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) {
- thrownew RuntimeException(e);
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- }
- }
- }
- }
-
- publicstaticvoid 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);
- }
- }
運行結果是這樣的:
二、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;
-
- publicclass Test {
- private DataSource dataSource;
-
- publicvoid setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
-
- publicvoid 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()});
- }
-
- publicstaticvoid 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了。
三、JdbcDaoSupport app
這個更簡單,不用new JdbcTemplate了。 this
- package jdbc;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
-
- publicclass Test extends JdbcDaoSupport {
- //JdbcDaoSupport類已經有了public final void setDataSource(DataSource dataSource)了
- //不用重寫也不能重寫
-
- publicvoid insert(User u) {
- String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql語句
- this.getJdbcTemplate().update(sql, new Object[]{u.getName(), u.getPassword()});
- }
-
- publicstaticvoid 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 url