0

Having a problem with this method. Suppose to return questions from a db where a chapter number dictates which questions to return.

It's returning :

ERROR: operator does not exist: character varying =? Hint: No operator matches the given name and argument type. You might need to add an explicit type cast.

Have Tried casting but nothing was working. Seems that it is taking the placeholder '?' as a literal value even though it isn't in double quotes.

Parameter is being passed from frontend but throwing back an Error:500 Internal Server Error i guess because of the 'operator not existing'.

Not sure where to go from here so any help is appreciated.

p.s. The parameter passed was an Integer before but again was asking me to cast it with no luck so changed to String and changed the Db column datatype to VARCHAR from smallint to try and get some sort of data type compatibility but nothing as of yet. Perhaps casting is the fix but not sure how to get it right myself.

public List<Question> selectQuestionsByChapterNumber(String chapternumber) {
    final String sql= "SELECT question FROM chapter_questions WHERE chapternumber=?";
    return jdbcTemplate.query(sql, (resultSet,i) ->{
        return new Question(
                resultSet.getInt("id"),
                resultSet.getString("chapternumber"),
                resultSet.getString("question"),
                resultSet.getString("answer"));
    });
    }
magee
  • 11
  • 2
  • 1
    I don't see any place where you are calling a method to replace the `?` placeholder with any value. Am I correct to assume that you want to replace `?` with the passed `String chapternumber`? In that case you be able to pass that value as a second parameter so that your sql prepared statements get correctly replaced: `jdbcTemplate.query(sql, new Object[] {chapternumber}, (resultSet,i) ->{ ...` – OH GOD SPIDERS Sep 07 '20 at 16:52
  • including 'new Object[] {chapternumber}' was the fix, thank you very much! – magee Sep 07 '20 at 17:07

0 Answers0