Invalid column index using PreparedStatement

后端 未结 2 1597
误落风尘
误落风尘 2021-01-28 01:35

I am trying to query same data. But the preparedStatement thrown an SQLException about wrong indexing even though the index start from 1 as the documentation said.

Here

2条回答
  •  温柔的废话
    2021-01-28 02:02

    Your question-mark place holder is inside single quotes, so it's being seen as a literal character - and not a place holder at all. When the statement is parsed the '%?%' is just a string and no bind variable is seen, so no bind variable can be set - there are no variables, so no variable with index 1.

    You can use concatenation to fix this:

        PreparedStatement searchKeyword = 
        connection.prepareStatement("SELECT title, registered FROM paper "
        + "WHERE title LIKE '%' || ? || '%'");
    

提交回复
热议问题