Spring的JdbcTemplate对数据库的增删改操作(备忘)

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

private JdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}


/**
* 添加
*/
public int addWxd(final Wxd wxd) {
		String sql = "insert into spjkwxd (xh,fxsj,fsdw) values (?,?,?)";
		int i = this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, wxd.getXh());
				ps.setString(2, wxd.getFxsj());
				ps.setString(3, wxd.getFsdw());
			}
		});
		return i;
	}
/**
* 修改
*/
public void updateWxd(final Wxd wxd) {
		String sql = "update spjkwxd set fxsj = ?,fsdw = ? where xh = ?";
		this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, wxd.getFxsj());
				ps.setString(2, wxd.getFsdw());
				ps.setString(3, wxd.getXh());
			}
		});
	}
/**
* 删除维修单
*/
	public int deleteWxd(String xh) {
		String sql = "delete from spjkwxd where xh = '" + xh + "'";
		int i = this.jdbcTemplate.update(sql);
		return i;
	}

相关推荐