tags:

views:

51

answers:

1

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();
    }
}
A: 

Note that your proposed solution is susceptible a SQL injection attack. Use java.sql.PreparedStatement instead, as described in Using Prepared Statements.

trashgod
Hi, trashgod, thanks for formatting my code ! Then, the app I'm developing executes user's SQL so SQL injection isn't a problem at all ;)
Miloud B.
Users like these? http://xkcd.com/327/ :-)
trashgod
Excellent ahahaha :p
Miloud B.
Btw, my app is targeted to developers, it allows running queries on a database that's why SQL inject. isn't an issue :)
Miloud B.