The value of PreparedStatement lies in the ability of the database itself to create an execution plan for the operator, which can be reused for arbitrary parameters and, thus, general (of course, this requires you to use the parameters in their status, for example
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?"); pstmt.setBigDecimal(1, 153833.00); pstmt.setInt(2, 110592);
If, on the other hand, you must use string concatenation to insert parameter values ββinto SQL code, the database cannot create a common execution plan. This way, it doesn't matter if you use PreparedStatement or Statement, for example.
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = 1200 WHERE ID = 3");
will not be able to take advantage of PreparedStatements.
Your question implies that you want to reuse the PreparedStatement object, which is optional. Of course, if you can use the PreparedStatement object to update multiple values, etc., this is a more efficient use of ressources resources. However, the service life (or at least the useful life) of the PreparedStatement is bound to the connection, so if you call conn.close (), the PreparedStatement is useless. However, most good drivers in a merge situation reuse the same PreparedStatement objects. In short, do not cache PreparedStatement regardless of connection.
Chris
source share