java arge模板_Choosing between jdbcTemplate.update(String sql,Object[] args,int[] argTypes) and jdbcTe...

I'm learning the combination of Spring Boot and jdbcTemplate for some basic crud operations, and trying to better understand which update method I should choose.

I understand that the following two class approaches (adapted from this post) will write the same record to the database.

Example 1:

public class InsertDemo {

private static final String sql =

"INSERT INTO records (title, " +

" release_date, " +

" artist_id, " +

" label_id, " +

" created) " +

"VALUES (?, ?, ?, ?, ?)";

private DataSource dataSource;

public InsertDemo(DataSource dataSource) {

this.dataSource = dataSource;

}

public void saveRecord(String title, Date releaseDate,

Integer artistId, Integer labelId) {

JdbcTemplate template = new JdbcTemplate(this.dataSource);

Object[] params = new Object[] {

title, releaseDate, artistId, labelId, new Date()

};

int[] types = new int[] {

Types.VARCHAR,

Types.DATE,

Types.INTEGER,

Types.INTEGER,

Types.DATE

};

int row = template.update(sql, params, types);

System.out.println(row + " row inserted.");

}

Example 2:

public class InsertDemo {

private static final String sql =

"INSERT INTO records (title, " +

" release_date, " +

" artist_id, " +

" label_id, " +

" created) " +

"VALUES (?, ?, ?, ?, ?)";

private DataSource dataSource;

public InsertDemo(DataSource dataSource) {

this.dataSource = dataSource;

}

public void saveRecord(String title, Date releaseDate,

Integer artistId, Integer labelId) {

JdbcTemplate template = new JdbcTemplate(this.dataSource);

Object[] params = new Object[] {

title, releaseDate, artistId, labelId, new Date()

};

int row = template.update(sql, params);

System.out.println(row + " row inserted.");

}

but I'm unclear on why I would/should use the first one that specifies argument types. I've read the javadoc but I'm still not sure why I would need to specify the types. What am I missing?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`jdbcTemplate.update()` 方法是 Spring 框架提供的一个核心方法,用于执行 SQL 更新语句。它支持以下参数类型: 1. `String sql`:SQL 更新语句。 2. `PreparedStatementSetter pss`:为 SQL 更新语句设置参数值的对象。 3. `Object... args`:SQL 更新语句中的参数值。 以下是 `jdbcTemplate.update()` 方法的几种常见用法: #### 1. 更新操作不需要传递参数值 如果 SQL 更新语句不需要传递参数值,可以直接使用 `jdbcTemplate.update(String sql)` 方法。例如: ```java String sql = "UPDATE your_table SET column1 = 'new_value' WHERE column2 = 'value2'"; jdbcTemplate.update(sql); ``` #### 2. 更新操作需要传递参数值 如果 SQL 更新语句需要传递参数值,可以使用 `jdbcTemplate.update(String sql, Object... args)` 方法。例如: ```java String sql = "UPDATE your_table SET column1 = ? WHERE column2 = ?"; Object[] args = new Object[] {"new_value", "value2"}; jdbcTemplate.update(sql, args); ``` #### 3. 更新操作需要传递 PreparedStatementSetter 对象 如果 SQL 更新语句需要传递 PreparedStatementSetter 对象,可以使用 `jdbcTemplate.update(String sql, PreparedStatementSetter pss)` 方法。例如: ```java String sql = "UPDATE your_table SET column1 = ? WHERE column2 = ?"; PreparedStatementSetter pss = new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, "new_value"); ps.setString(2, "value2"); } }; jdbcTemplate.update(sql, pss); ``` #### 4. 更新操作需要获取更新后的自增长主键值 如果 SQL 更新语句需要获取更新后的自增长主键值,可以使用 `jdbcTemplate.update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)` 方法。例如: ```java String sql = "INSERT INTO your_table (column1, column2) VALUES (?, ?)"; PreparedStatementCreator psc = new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement(sql, new String[] {"id"}); ps.setString(1, "value1"); ps.setString(2, "value2"); return ps; } }; KeyHolder generatedKeyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(psc, generatedKeyHolder); int generatedId = generatedKeyHolder.getKey().intValue(); // 获取自增长主键值 ``` 以上是 `jdbcTemplate.update()` 方法的几种常见用法,它们可以满足大多数的 SQL 更新操作需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值