Database access using parametrized and prepared statements
Simple question. I am using Java and MySQL database. I want to write database access methods what would use PreparedStatement method and would be parametrized, such as:
public DBResult selectQueryWithParameters(String SQL, Object... params)
I know that you can do this for select queries, but what about DROP, CREATE and ALTER queries what are using execute(S开发者_JAVA百科QL) method. Likewise you may execute UPDATE, INSERT and DELETE queries what are using executeUpdate(SQL) method. Can I write parametrized methods for these queries? Best regards
You can use Connection's prepareStatement method to prepare a statement. Then you can use setInt, setString, setXX methods to set the parameters. This works for SELECT, UPDATE, INSERT, and DELETE.
Why would you want a PreparedStatement for DROP, CREATE, ALTER? CREATE TABLE stuff ( message VARCHAR(?) ) ;
It does not make much sense to me.
精彩评论