使用带有JDBCTemplate的预准备语句

26 java spring jdbc prepared-statement jdbctemplate

我正在使用JDBC模板,并希望使用预准备语句从数据库中读取.我在.csv文件中迭代多行,并在每一行上执行一些带有相应值的SQL select查询.

我想加快我对数据库的阅读速度,但我不知道如何让JDBC模板与预处理语句一起工作.

PreparedStatementCreatorPreparedStatementSetter.在本示例中,它们都是使用匿名内部类创建的.但是在PreparedStatementSetter类中,我无法访问我想在预准备语句中设置的值.

因为我正在迭代.csv文件,所以我不能将它们硬编码为String,因为我不知道它们.我也无法将它们传递给PreparedStatementSetter,因为构造函数没有参数.将我的价值观设定为最终值也是愚蠢的.

我习惯于创建准备好的语句非常简单.就像是

PreparedStatement updateSales = con.prepareStatement(
    "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75); 
updateSales.setString(2, "Colombian"); 
updateSales.executeUpdate():
Run Code Online (Sandbox Code Playgroud)

就像在这个Java教程中一样.

mez*_*zmo 28

默认情况下,JDBCTemplate做自己的PreparedStatement内部,如果你只是使用.update(String sql, Object ... args)的形式.Spring和您的数据库将为您管理已编译的查询,因此您不必担心打开,关闭,资源保护等等.其中一个保存优点是Spring.Spring 2.5的文档链接.希望它能让事情更加清晰.此外,语句缓存可以在JDBC级别完成,就像至少部分Oracle的JDBC驱动程序一样. 这比我能胜任的细节要多得多.

  • 但是我想对数据库执行select,而不是更新.在Spring参考中写了http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/JdbcTemplate.html,只有`update`才能插入,更新或删除可以执行. (4认同)

小智 19

class Main {
    public static void main(String args[]) throws Exception {
        ApplicationContext ac = new
          ClassPathXmlApplicationContext("context.xml", Main.class);
        DataSource dataSource = (DataSource) ac.getBean("dataSource");
// DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        String prasobhName = 
        jdbcTemplate.query(
           "select first_name from customer where last_name like ?",
            new PreparedStatementSetter() {
              public void setValues(PreparedStatement preparedStatement) throws
                SQLException {
                  preparedStatement.setString(1, "nair%");
              }
            }, 
            new ResultSetExtractor<Long>() {
              public Long extractData(ResultSet resultSet) throws SQLException,
                DataAccessException {
                  if (resultSet.next()) {
                      return resultSet.getLong(1);
                  }
                  return null;
              }
            }
        );
        System.out.println(machaceksName);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 查询部分为java 8 lambda样式:`jdbcTemplate.query(sql,ps - > ps.setString(1,"value"),(rs,i) - > rs.getLong(1)` (3认同)

Kev*_*vin 10

请尝试以下方法:

PreparedStatementCreator creator = new PreparedStatementCreator() {
    @Override
    public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
        PreparedStatement updateSales = con.prepareStatement(
        "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
        updateSales.setInt(1, 75); 
        updateSales.setString(2, "Colombian"); 
        return updateSales;
    }
};
Run Code Online (Sandbox Code Playgroud)


小智 2

我现在尝试使用 a 的 select 语句PreparedStatement,但事实证明它并不比 Jdbc 模板快。也许,正如 mezmo 所建议的,它会自动创建准备好的语句。

SELECT无论如何,我的 sql如此缓慢的原因是另一个原因。在子句中,当我只想找到完全匹配时,WHERE我总是使用运算符。LIKE正如我发现的那样,LIKE搜索模式非常慢。

我现在正在使用操作员=,速度要快得多。