日期:2014-05-16 浏览次数:20568 次
@Autowired private JdbcTemplate jdbcTemplate; ........ String sql="......."; jdbcTemplate.execute(sql);
final String sql = "INSERT INTO t_forum(forum_name,forum_desc) VALUES(?,?)"; Object[] params = new Object[] { forum.getForumName(), forum.getForumDesc() }; jdbcTemplate.update(sql, params);
KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, forum.getForumName()); ps.setString(2, forum.getForumDesc()); return ps; } }, keyHolder); forum.setForumId(keyHolder.getKey().intValue());
String sql = "SELECT ......"; final List<Forum> forums = new ArrayList<Forum>(); jdbcTemplate.query(sql,new Object[]{fromId,toId},new RowCallbackHandler(){ public void processRow(ResultSet rs) throws SQLException { Forum forum = new Forum(); forum.setForumId(rs.getInt("forum_id")); forum.setForumName(rs.getString("forum_name")); forum.setForumDesc(rs.getString("forum_desc")); forums.add(forum); }}); return forums;
@Override public Person getPerson(String lastName) { String query = "select personId, personFirstName, personLastName from personTbl where personLastName = ?"; return this.getJdbcTemplate().queryForObject(query, new PersonMapper(), lastName); }
public int getNumberOfPeople() { int rowCount = this.getJdbcTemplate().queryForInt("select count(0) from personTbl"); return rowCount; }
String firstName = this.getJdbcTemplate().queryForObject( "select personFirstName from personTbl where personLastName = ?", String.class, lastName ) ;
String sql = "select count(0) from personTbl where personFirstName = :first_name"; SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName); return this.getNamedParameterJdbcTemplate().queryForInt(sql, namedParameters);
public List<Person> getPerson(Person pe