What does “precompiling” a statement do, because I have seen that
if I write a prepared statement with a bad SQL syntax that compilation does not report
any problem!
So if precompiling a prepared statement doesn’t check for syntax validity what really does it do?
Creating a
PreparedStatementsmay or may not involve SQL syntax validation or even DB server roundtrips, that depends entirely on the JDBC driver used. Some drivers will do a roundtrip or validate, others will not.So on some JDBC drivers a
PreparedStatementis no more “prepared” than a normalStatement. (In other words: with some JDBC drivers aPreparedStatementrepresents a server-side resource (similar toConnection), while on others it’s a pure client-side construct).An important difference, however is that a
PreparedStatementwill help you handle dynamic parameter values in a way that is guaranteed to avoid any escaping or formatting issues that you would have if you try to insert the values into the SQL statement string manually and execute it using a normalStatement.That feature is indepdendent from the choice of “preparing” the statement beforehand or not, so it’s provided by every JDBC driver, even if it doesn’t do any other preparation steps.