4 Replies Latest reply: Sep 10, 2012 11:09 AM by Joe Weinstein-Oracle RSS

    Execute,ExecuteQuery,ExecuteUpdate

    956925

      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
          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
            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
              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-Oracle
                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