This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am using commons.dbutils.QueryRunner.update(String) method to update a row. This works perfectly fine, if String parameter query doesn't contain any "?" marks charecter. If String contains "?", then update throws an SQLException -No value specified for parameter 1
I tried escaping "?" with backslash "\". Can someone please tell me how can I escape "?" while using QueryRunner.update?
Normally, I'd say call stmt.setEscapeProcessing(false) before executing the query, but since you are using QueryRunner I'm guessing you don't have access to the statement object. Another option is to set the ? and its surrounding parameters as a ? such as: In this way, the first question mark is replaced by the first item in the parameters array, which is untouched.
Not really, he specifically uses update() instead of select, but either way, how is this solution any different? Should still work since after the parameters are inserted, the processing is complete. Keep in mind this code will be sent to the database as: Select publisher from books where title='What is the name of this book?'
The problem with sending the second the statement directly is that JDBC PreparedStatement muddles things up and if you can't turn processing off on the statement, this is the next best thing. [ July 08, 2008: Message edited by: Scott Selikoff ]
The fact that you are using prepare statement, you shouldn't be hardcoding the dynamic content in it instead should be making use of parameters. That is what they were designed for.
However if you still want to hardcode the values in the preparedStatement itself, you can enclose the Strings in the single quotes ('), and it should work fine. Check out this example.
[ July 08, 2008: Message edited by: Santhosh Kumar ]
Joined: Dec 29, 2006
Thanks for the reply.
My problem is bit different. I have to update a row in database not select it. It is something like this String strQuery = "UPDATE tablename set columnname = 'there is ? in middle'"; QueryRunner queryRunner = new QueryRunner(this.getDataSource()); queryRunner.update(strQuery);
This is working perfectly fine for all other strings. It is throwing exception only when there is a question mark present in String like given above.