4 Replies Latest reply on Sep 10, 2012 4:09 PM by Joe Weinstein-Oracle



      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
          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
            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
              Kayaman wrote:
              I don't recall ever using the general execute method.
              Accept for <tt>CallableStatement</tt>s.

              • 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!
                } 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