1、使用JdbcTemplate的execute()方法执行SQL语句
2、如果是UPDATE或INSERT,可以用update()方法。
- jdbcTemplate.update("INSERT INTO USER VALUES('"
- + user.getId() + "', '"
- + user.getName() + "', '"
- + user.getSex() + "', '"
- + user.getAge() + "')");
3、带参数的更新
jdbcTemplate.update("UPDATE USER SET name = ? WHERE user_id = ?", new Object[] {name, id});
4、使用JdbcTemplate进行查询时,使用queryForXXX()等方法
- int count = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM USER");
- String name = (String) jdbcTemplate.queryForObject("SELECT name FROM USER WHERE user_id = ?", new Object[] {id}, java.lang.String.class);
Java代码
- List rows = jdbcTemplate.queryForList("SELECT * FROM USER");
- List rows = jdbcTemplate.queryForList("SELECT * FROM USER");
- Iterator it = rows.iterator();
- while(it.hasNext()) {
- Map userMap = (Map) it.next();
- System.out.print(userMap.get("user_id") + "\t");
- System.out.print(userMap.get("name") + "\t");
- System.out.print(userMap.get("sex") + "\t");
- System.out.println(userMap.get("age") + "\t");
- }
JdbcTemplate将我们使用的JDBC的流程封装起来,包括了异常的捕捉、SQL的执行、查询结果的转换等等。spring大量使用Template Method模式来封装固定流程的动作,XXXTemplate等类别都是基于这种方式的实现。
除了大量使用Template Method来封装一些底层的操作细节,spring也大量使用callback方式类回调相关类别的方法以提供JDBC相关类别的功能,使传统的JDBC的使用者也能清楚了解spring所提供的相关封装类别方法的使用。
JDBC的PreparedStatement
final String id = user.getId();
final String name = user.getName();
final String sex = user.getSex() + "";
final int age = user.getAge();
jdbcTemplate.update("INSERT INTO USER VALUES(?, ?, ?, ?)",
new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, id);
ps.setString(2, name);
ps.setString(3, sex);
ps.setInt(4, age);
} });
1.final User user = new User();
2.jdbcTemplate.query("SELECT * FROM USER WHERE user_id = ?",
3.new Object[] {id},
4.new RowCallbackHandler() {
5.public void processRow(ResultSet rs) throws SQLException {
6.user.setId(rs.getString("user_id"));
7.user.setName(rs.getString("name"));
8.user.setSex(rs.getString("sex").charAt(0));
9.user.setAge(rs.getInt("age"));
10.}
11.});
1.class UserRowMapper implements RowMapper {
2.
3.public Object mapRow(ResultSet rs, int index) throws SQLException {
4.
5.User user = new User();
6.user.setId(rs.getString("user_id"));
7.user.setName(rs.getString("name"));
8.user.setSex(rs.getString("sex").charAt(0));
9.user.setAge(rs.getInt("age"));
10.return user;
11.}
12.}
13.
14.public List findAllByRowMapperResultReader() {
15.
16.String sql = "SELECT * FROM USER";
17.
18.return jdbcTemplate.query(sql, new RowMapperResultReader(new UserRowMapper()));
19.
20.}
在getUser(id)里面使用UserRowMapper
1.public User getUser(final String id) throws DataAccessException {
2.String sql = "SELECT * FROM USER WHERE user_id=?";
3.final Object[] params = new Object[] { id };
4.List list = jdbcTemplate.query(sql, params, new RowMapperResultReader(new UserRowMapper()));
5.return (User) list.get(0);
6.}
网上收集
org.springframework.jdbc.core.PreparedStatementCreator 返回预编译SQL 不能于Object[]一起用
- public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
- return con.prepareStatement(sql);
- }
1.增删改
org.springframework.jdbc.core.JdbcTemplate 类(必须指定数据源dataSource)
或
template.update("insert into web_person values(?,?,?)",new PreparedStatementSetter(){ //匿名内部类 只能访问外部最终局部变量 2.public void setValues(PreparedStatement ps) throws SQLException { 3.ps.setInt(index++,3); 4.}); 5. 6. 1.org.springframework.jdbc.core.PreparedStatementSetter //接口 处理预编译SQL 2.public void setValues(PreparedStatement ps) throws SQLException { 3.ps.setInt(index++,3); 4.} 5. 6.public void setValues(PreparedStatement ps) throws SQLException { 7.ps.setInt(index++,3); 8.}
2.查询JdbcTemplate.query(String,[Object[]/PreparedStatementSetter],RowMapper/RowCallbackHandler)
org.springframework.jdbc.core.RowMapper 记录映射接口 处理结果集
1.public Object mapRow(ResultSet rs, int arg1) throws SQLException { //int表当前行数 2.person.setId(rs.getInt("id")); 3.} 4.List template.query("select * from web_person where id=?",Object[],RowMapper);
org.springframework.jdbc.core.RowCallbackHandler 记录回调管理器接口 处理结果集
1.template.query("select * from web_person where id=?",Object[],new RowCallbackHandler(){
2.public void processRow(ResultSet rs) throws SQLException {
3.person.setId(rs.getInt("id"));
4.});