This discussion is archived
4 Replies Latest reply: Sep 10, 2012 9:09 AM by Joe Weinstein RSS

Execute,ExecuteQuery,ExecuteUpdate

956925 Newbie
Currently Being Moderated
Hi,
What is the difference between execute,execute query and execute update?
where exactly we can use this methods?

Thanks and regards
  • 1. Re: Execute,ExecuteQuery,ExecuteUpdate
    Kayaman Guru
    Currently Being Moderated
    You should read the javadocs, they explain everything.
    The difference between executeQuery and executeUpdate is easy, I don't recall ever using the general execute method.
  • 2. Re: Execute,ExecuteQuery,ExecuteUpdate
    gimbal2 Guru
    Currently Being Moderated
    Kayaman wrote:
    You should read the javadocs, they explain everything.
    To know how something works... read the documentation that explains it. What a wild concept.

    What makes me sad: you SHOULD be captain obvious. But you're not.
  • 3. Re: Execute,ExecuteQuery,ExecuteUpdate
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Kayaman wrote:
    I don't recall ever using the general execute method.
    Accept for <tt>CallableStatement</tt>s.

    bye
    TPD
  • 4. Re: Execute,ExecuteQuery,ExecuteUpdate
    Joe Weinstein Expert
    Currently Being Moderated
    PreparedStatement.execute() is perfect for processing a stored procedure or SQL that will return
    an arbitrary/unknown series of resultsets and/or update counts. Sybase and SQLServer can do this,
    so it's important with them. It returns true if the first result is a ResultSet object; false if the first
    result is an update count or there is no result. Here is the canonical JDBC code for processing all
    the returns from an arbitrary procedure or SQL:

    boolean getResultSetNow = ps.execute();
    int updateCount = -1;

    while (true) { // handle all in-line results from any procedure
    if (getResultSetNow) {
    ResultSet r = ps.getResultSet();
    while (r.next()) {
    // fully process result set before calling getMoreResults() again!
    }
    r.close();
    } else {
    updateCount = ps.getUpdateCount();
    if (updateCount != -1) { // it's a valid update count
    if (you want) System.out.println("Processing an update count of " + updateCount);
    }
    }
    if ((!getResultSetNow) && (updateCount == -1)) break; // done with loop
    getResultSetNow = ps.getMoreResults();
    }
    // if there are output parameters get them now after the loop

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points