java Spring-Jdbc 模板和准备好的语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27717906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:16:11  来源:igfitidea点击:

Spring-Jdbc Template and Prepared statement

javaprepared-statementspring-jdbc

提问by andy007

Do I need to close Prepared Statement and Connection (jt.getDataSource().getConnection()) when using Spring-Jdbc Template? Or they will be closed by Template mechanizm?

使用 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);

    }

回答by Lokeswaraswamy Gubba

By default, the JDBCTemplate does its own PreparedStatement internally, if you just use the .update(String sql, Object ... args) form. Spring, and your database, will manage the compiled query for you, so you don't have to worry about opening, closing, resource protection, etc.

默认情况下,JDBCTemplate 在内部执行自己的 PreparedStatement,如果您只使用 .update(String sql, Object ... args) 形式。Spring,还有你的数据库,会为你管理编译好的查询,让你不用担心打开、关闭、资源保护等问题。

回答by Thomas Risberg

If you want to use the PreparedStatementCreator, instead of calling the JdbcTemplatemethods that take an SQL statement as a parameter, you should use the JdbcTemplatemethod that takes your PreparedStatementCreatoras a parameter.

如果您想使用PreparedStatementCreator, 而不是调用将JdbcTemplateSQL 语句作为参数的JdbcTemplate方法,您应该使用将您PreparedStatementCreator作为参数的方法。

In your example remove:

在您的示例中删除:

PreparedStatement updateIgnor = creator.createPreparedStatement(jt.getDataSource().getConnection());
int k = updateIgnor.executeUpdate();

and replace it with:

并将其替换为:

int k = jt.update(creator);

That way the JdbcTemplatewill handle the statement and connection resources plus any transaction management and close the resources as needed.

这样,JdbcTemplate将处理语句和连接资源以及任何事务管理并根据需要关闭资源。

回答by Buru

Using spring JDBC Template you don't need to close or open connections. it will handle and exceptions internally.

使用 spring JDBC 模板,您不需要关闭或打开连接。它将在内部处理和异常。

Object[] parameters={boolean_value,date_value,int_value};
int[] types={Types.BOOLEAN,Types.TIMESTAMP,Types.INTEGER};
rowsAffected=jdbcTemplate.update("insert into table(col1,col2,col3) values(?,?,?)",parameters,types);