0

Java DB2 Sql command throws an error if i have a ' in my String i tried to replace it with a escape sequence but it would not work on the DB2 end any help ? I want the ' to be present in the String.

Error

com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-10, SQLSTATE=42603, SQLERRMC=', DRIVER=3.63.123

Java

String FUNC_VP = "Chris O'Connor/Henry/George";
String myQuery = "SELECT DISTINCT(VICE_PRES) FROM EMP_HC WHERE FUNC_VP ='"+funcvp_name.replace("'", "/'")+"'";  
1
  • Unrelated, but: distinct is not a function Commented Apr 12, 2014 at 8:12

2 Answers 2

8

Use a PreparedStatement and its setParameter() methods. It will handle things like escaping for you, and you'll never have to wonder how a particular database might handle things. It also prevents SQL injection, so there's no good reason not to use it.

Sign up to request clarification or add additional context in comments.

Comments

1

Try

String FUNC_VP = "Chris O''Connor/Henry/George";

To escape an apostrohe on DB2 it seems you have to use two apostrophes

1 Comment

(Not downvoting because this is technically correct, but...) No, don't escape them yourself - this is just papering over a larger problem: the op may be (probably is) open to SQL Injection. Use prepared statements (As the other answer states), and you don't even have to do the escaping yourself. There should be no reason to ever need to do the escaping yourself for a statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.