skip to Main Content

is it possible to run this query

jdbcTemplate.update(
           "UPDATE SOME_TABLE SET  MY_NAME=?,MY_FILE=?" +
                  "WHERE MY_ID = ?", 
           new Object[] {
               "TestME",null,ID
                   
               });

Note that there is no space between the 2nd ? and WHERE keyword.
I can add a space in between and make it run but we have such instance in many places so wanted to check if we can avoid code changes

2

Answers


  1. No, it is not possible. The space is required to separate the parameter from the WHERE. Fix your code. It should be simple with a find and replace. Search for "WHERE and replace with " WHERE.

    Login or Signup to reply.
  2. Well, why don’t you test it?

    My test in plain JDBC shows a valid statement. I’m using Groovy script with JDBC, but assume that the same behaviour will occure in jdbcTemplate. The SQL string is important.

    SQL = "UPDATE SOME_TABLE SET  MY_NAME=?,MY_FILE=?" +
                      "WHERE MY_ID = ?"
    println SQL                  
    def stmt = cn.prepareStatement(SQL)
    
    stmt.setString(1,'x')
    stmt.setString(2,'y')
    stmt.setInt(3,1)                  
    Integer rowCount = stmt.executeUpdate()
    println "update rowCount= $rowCount "
    

    the output is

    UPDATE SOME_TABLE SET  MY_NAME=?,MY_FILE=?WHERE MY_ID = ?
    update rowCount= 1
    

    And by the way SQL realy do not requires blank separators in each possition. This is also a valid statement.

     UPDATE SOME_TABLE SET  MY_NAME='xx',MY_FILE='yy'WHERE MY_ID = 1;
    

    Tested in PostgreSQL 14.4

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search