Invalid column index using PreparedStatement

后端 未结 2 1590
误落风尘
误落风尘 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 01:59

    Print out the query string before executing it:

    SELECT title, registered FROM paperWHERE title LIKE '%?%'
    

    The answer should be obvious.

    You need spaces to delimit keywords.

    0 讨论(0)
  • 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 '%' || ? || '%'");
    
    0 讨论(0)
提交回复
热议问题