Update 2 The down-vote was deserved and encouraged me to write a more robust test: Using java 1.8, ojdbc8.jar against a non-production, Oracle 12C database where I am normally the sole user,
I ran 10 iterations of 100 selects: 50 using a PreparedStatement and 50 using a Statement (building the query with String concatenation).
To try and rule out some kind of database caching, I then ran the test again but switched the order, running the Statements first followed by the PreparedStatements.
There was no significant difference in performance, regardless of order. See below for results (in milliseconds with averages and standard deviations).
UPDATE: Thanks for the all the input. I updated the title. I found a lot of platitudes while researching this ("PreparedStatements are always faster", "PreparedStatements are always slower") but nothing definitive so, I ended up writing a test and found that, for this case, PreparedStatements were consistently ~25% slower.
Initial question: I have a static method with a simple query that checks if an ID is already in the database:
public static boolean isPersisted(Integer id) {
String sql = "select id from table where id = "+id+"";
I understand they can guard against SQL injection, and I know they offer big benefits when looping over an insert with a list of IDs (for example) but, is there any performance/memory benefit to using a prepared statement here?