如何从 JdbcTemplate 获取受影响的行数?
How to get number of affected rows from JdbcTemplate?
我正在使用 spring JdbcTemplate
执行 sql 查询:
JdbcTemplate template = new JdbcTemplate(ds);
template.execute(sqlInsert); //returns void
如何获得受影响的行数,因为 execute()
方法 returns 无效?
对于这种情况,您或许可以使用 JdbcTemplate.update()
。这将 return 更新或删除的行数。
调用JdbcTemplate
的update方法。它会给你受影响的行数作为 return 值。
update
public int update(PreparedStatementCreator psc)
throws DataAccessException
Description copied from interface: JdbcOperations
Issue a single SQL update operation (such as
an insert, update or delete statement) using a
PreparedStatementCreator
to provide SQL and any required parameters.
A PreparedStatementCreator
can either be implemented directly or
configured through a PreparedStatementCreatorFactory
.
Specified by:
update in interface JdbcOperations
Parameters:
psc - object that provides SQL and any necessary parameters
Returns:
the number of rows affected
Throws:
DataAccessException
- if there is any problem issuing the update
See Also:
PreparedStatementCreatorFactory
我正在使用 spring JdbcTemplate
执行 sql 查询:
JdbcTemplate template = new JdbcTemplate(ds);
template.execute(sqlInsert); //returns void
如何获得受影响的行数,因为 execute()
方法 returns 无效?
对于这种情况,您或许可以使用 JdbcTemplate.update()
。这将 return 更新或删除的行数。
调用JdbcTemplate
的update方法。它会给你受影响的行数作为 return 值。
update
public int update(PreparedStatementCreator psc) throws DataAccessException
Description copied from interface:
JdbcOperations
Issue a single SQL update operation (such as an insert, update or delete statement) using a
PreparedStatementCreator
to provide SQL and any required parameters. APreparedStatementCreator
can either be implemented directly or configured through aPreparedStatementCreatorFactory
.Specified by:
update in interfaceJdbcOperations
Parameters:
psc - object that provides SQL and any necessary parametersReturns:
the number of rows affectedThrows:
DataAccessException
- if there is any problem issuing the updateSee Also:
PreparedStatementCreatorFactory