Java

Java

Made by DeepSource

Prepared statements should not be created within a loop JAVA-S0329

Performance
Critical

The method calls Connection.prepareStatement inside the loop passing the constant arguments.

If the PreparedStatement should be executed several times there's no reason to recreate it for each loop iteration.

Creating a prepared statement causes the server side database engine to allocate resources for the purpose of efficient execution. If the same statement is recreated for no reason, it could drive the engine to exhaust allocated memory unnecessarily. Modern implementations tend to cache such statements to prevent this kind of exhaustion from occurring (Oracle DB for example) but this behavior must not be relied on.

Move this call outside the loop.