Besides preventing SQL injection, formatting portability (which you can't get from Statement
), performance is the obvious reason. However, PreparedStatement
doesn't come without any penalty. For example, it is generally slower than Statement
if running only once, as there is some overhead. So the general idea is PreparedStatement
should be used when you are performing the same query many many times. However, how much overhead is very database server implementation-specific, so exactly when to choose PreparedStatement
over Statement
, from performance consideration, should really be based on your actual experience/experiments of a specific database server.