First, John replies that you have to go with the most obvious solution until performance is considered a problem, of course, is the right approach in general.
I don't think your performance problems are inappropriate. Of course, I saw that precompiled complex statements do not dramatically affect performance (on MS-SQL 2000). The reason is that the statement was so complex that it had several potential execution paths depending on the parameters, but the compilation was blocked for one set of parameters, and the next set of parameters was too slow, while recompiling would make the recalculation of the execution plan more suitable for a different set of parameters.
But this problem is very far, until you see it in practice.
The main problem here is that parameter escaping is a database specification, so if the JDBC driver for your database does not give you something non-standard for this (unlikely), you will have to have another library or another one that is very specific for this Database.
From the wording of your question, it doesn’t sound like your performance problems still go so far as to find a solution (or develop) such a solution.
It should also be noted that although JDBC drivers may not all behave this way, technically according to the specification it is assumed that the precompilation should be cached in the PreparedStatement object, and if you throw it away and get a new PreparedStatement every time, it should not actually cache anything, so the whole problem can be dumb and should be investigated for your specific JDBC driver.
From the specification:
An SQL statement with or without IN parameters can be precompiled and stored in the PreparedStatement object. Then this object can be used to efficiently execute this statement several times.
source share