JDBC general query execution
Is there a way, in JDBC, to execute a generic query ? I mean run something like execute(String strSql) where strSql could be a SELECT, an INSERT, an UPDATE,a CREATE,... or whatever.
If no, how would you fix this up ?
Proposed solution:
@Override
public void execQuery(String Query) throws SQLException {
this.statement = this.connection.createStatement();
if (this.statement.execute(Query)) {
t开发者_如何学JAVAhis.resultset = this.statement.getResultSet();
}
}
Note that your proposed solution is susceptible a SQL injection attack. Use java.sql.PreparedStatement
instead, as described in Using Prepared Statements.
精彩评论