Java PreparedStatement:以任何方式使用索引或命名参数?

时间:2019-02-28 09:25:52

标签: java jdbc prepared-statement

我正在处理多次在PreparedStatement中使用参数的情况:

SELECT * FROM SOME_TABLE
WHERE (A LIKE 'searchterm1' OR B LIKE 'searchterm1' OR C LIKE 'searchterm1')
AND   (A LIKE 'searchterm2' OR B LIKE 'searchterm2' OR C LIKE 'searchterm2')

填充参数时,我必须多次提供每个参数(searchterm1和searchterm2):

try (PreparedStatement statement = connection.prepareStatement(
        "SELECT * FROM SOME_TABLE " +
                " WHERE (A LIKE ? OR B LIKE ? OR C LIKE ?) " +
                " AND   (A LIKE ? OR B LIKE ? OR C LIKE ?")) {
    statement.setString(1, "searchterm1");
    statement.setString(2, "searchterm1");
    statement.setString(3, "searchterm1");
    statement.setString(4, "searchterm2");
    statement.setString(5, "searchterm2");
    statement.setString(6, "searchterm2");
    ResultSet resultSet = statement.executeQuery();
}

是否有办法在准备好的语句查询中多次引用同一参数,所以我只需要设置一次(每个参数)? 所以像

  • 命名参数::searchterm1
  • 索引参数:?1

0 个答案:

没有答案