Java SQL Escape without using setString <p>Is there a built-in method to escape a string for SQL? I would use setString, but it happens I am using setString multiple times in the same combined SQL statement and it would be better performance (I think) if the escape happened only once instead of each time I say setString. If I had the escaped string in a variable, I could re-use it.</p> <p>Is there no way to do this in Java?</p> <p>Current method, multi-source search. In reality they are three entirely different where statements including joins, but for this example I will just show the same where for each table.</p> <pre><code>String q = '%' + request.getParameter("search") + '%'; PreparedStatement s = s("SELECT a,b,c FROM table1 where a = ? UNION select a,b,c from table2 where a = ? UNION select a,b,c FROM table3 where a = ?"); s.setString(1, q); s.setString(2, q); s.setString(3, q); ResultSet r = s.executeQuery(); </code></pre> <p>I know this is not a big deal, but I like to make things efficient and also there are situations where it is more readable to use <code>" + quote(s) + "</code> instead of <code>?</code> and then somewhere down the line you find <code>setString</code>.</p>