问题
I was going through differences between Statement and PreparedStatement in JDBC and saw so many advantages here and here with PreparedStatement in comparison with Statement.
Some of my colleague was asking why we still need Statement and why it is not deprecated looking at the advantages of PreparedStatement.
So is there any reason why we still have the Statement in JDBC API?
回答1:
PreparedStatement
is used to handle the dynamic SQL queries, where Statement
is used to handle static SQL queries.
回答2:
So is there any reason why we still have the Statement in JDBC API?
Yes because it's in the SQL client-server API. If non-prepared statements were to be removed, then a feature of SQL would be missing from JDBC.
As alluded to in other answers, PreparedStatement has no advantages whatsoever if there are no dynamic parameters. For those cases it is slightly more concise to use a non-prepared statement.
Completely unrleated to those matters is the fact that PreparedStatement is a Suberinterface of Statement and so the latter cannot be removed without redesigning the API. That is more to do with the Java API though and not to do with SQL.
来源:https://stackoverflow.com/questions/31768730/are-there-any-disadvantages-using-preparedstatement-compared-to-statement