Updating the Database
下面的例子更新了某个主键的一个列:
import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class ExecuteAnUpdate {private JdbcTemplate jdbcTemplate;public void setDataSource(DataSource dataSource) {this.jdbcTemplate = new JdbcTemplate(dataSource);}public void setName(int id, String name) {this.jdbcTemplate.update("update mytable set name = ? where id = ?", name, id);}}
在前面的例子中,一个 SQL 语句有行参数的占位符。你可以把参数值作为 varargs 传入,或者作为对象的数组传入。因此,你应该在基元包装类中显式地包装基元,或者你应该使用自动装箱。
