收录日期:2020/10/31 21:26:34 时间:2010-09-07 03:19:16 标签:java,sql,jdbc

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)) {
        this.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.