使用Spring-Jdbc模板时是否需要关闭Prepared Statement和Connection(jt.getDataSource().getConnection())?或者它们会被模板机制关闭?
public void updateRow() throws SQLException {
final int i = 100;
final int y = 2;
PreparedStatementCreator creator = new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement updateSales = con.prepareStatement(
"update ignor set ignored_id=? where id=?");
updateSales.setInt(1, i);
updateSales.setInt(2, y);
return updateSales;
}
};
PreparedStatement updateIgnor = creator.createPreparedStatement(jt.getDataSource().getConnection());
int k = updateIgnor.executeUpdate();
System.out.println("rows updated = " + k);
}
最佳答案
默认情况下,如果您仅使用 .update(String sql, Object ... args) 形式,则 JDBCTemplate 在内部执行自己的PreparedStatement。 Spring 和您的数据库将为您管理已编译的查询,因此您不必担心打开、关闭、资源保护等问题。
关于java - Spring-Jdbc模板和Prepared语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27717906/