I would like to create a JDBC PreparedStatement, for example:
SELECT URL,LOCATE ( '?', URL ) pos FROM Links WHERE pageId=? ORDER BY pos ASC
Where is the 1st ? is a literal, and the second ? is a parameter. I could use CHAR(63) instead of '?' , but I think an extra function call will slow down SQL execution. Is there any way to avoid this 1st ? ?
Edit:
The following code checks dkatzel's statement that the character ? in a line is not considered a marker:
public class Test { public static void main(String[] args) throws SQLException { Connection conn = DriverManager.getConnection("jdbc:h2:mem:test"); Statement stmt = conn.createStatement(); stmt.executeUpdate("CREATE TABLE Links(URL VARCHAR(255) PRIMARY KEY,pageId BIGINT)"); stmt.executeUpdate("INSERT INTO Links(URL,pageId) VALUES('http://foo.bar?baz',1)"); stmt.executeUpdate("INSERT INTO Links(URL,pageId) VALUES('http://foo.bar/baz',1)"); stmt.close(); PreparedStatement ps = conn .prepareStatement("SELECT URL,LOCATE ( '?', URL ) pos FROM Links WHERE pageId=? ORDER BY pos ASC"); ps.setLong(1, 1); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getString(1) + ":" + rs.getInt(2)); } rs.close(); ps.close(); conn.close(); } }
Exit:
http://foo.bar/baz:0 http://foo.bar?baz:15
It seems that dkatzel is true. I searched for JDBC Spec and could not find mention of what parameter marker ? it is ignored if it is enclosed in quotation marks, but several implementations of the PreparedStatement parsers that I found ( MySql , c-JDBC , H2 ) all seem to exclude the text in single quotes from consideration as parameter markers.
java sql jdbc prepared-statement
Mike godin
source share