JDBC GS execute method summary

Statement's execute() methods for SQL DML and SQL DDL queries?

  • This method (execute()) can executes any SQL statement, which may return multiple results.
  • Normally you can ignore this multiple results futures unless you are executing a stored procedure that you know may return multiple results.
    The Statement interface has these methods for that:
    Methods in Statement interface Description
    boolean execute(String sql) Executes the given SQL statement, and returns true if the first result is a ResultSet object; false if it is an update count or there are no results.
    boolean execute(String sql, int autoGeneratedKeys) Same as execute(String sql) but for a SQL INSERT you can signals the driver a given flag about whether the auto-generated keys produced by this Statement object should be made available for retrieval.
    boolean execute(String sql, int[] columnIndexes) Same as execute(String sql) but for a SQL INSERT you can signals the driver that the auto-generated keys indicated in the given array should be made available for retrieval. The array contains the indexes (starting with 1 for the first) of the columns.
    boolean execute(String sql, String[] columnNames) Same as execute(String sql) but for a SQL INSERT you can signals the driver that the auto-generated keys indicated in the given array should be made available for retrieval. The array contains the names of the columns.
  • As SQL statements used with an execute() method can lead to a ResultSet result, then you may be need to read the ResultSet data.
  • The three last methods above can result in a possibility to retrieve the auto generated key for a INSERT SQL.
    The Statement interface has some methods that help you to retrieve such things after an SQL execute() method:
    Methods in Statement interface Description
    ResultSet getResultSet() Retrieves the current result as a ResultSet object. This method should be called only once per result.
    int getUpdateCount() Retrieves the current result as an update count; if the result is a ResultSet object or there are no more results, -1 is returned. This method should be called only once per result.
    boolean getMoreResults() Moves to this Statement object's next result, returns true if it is a ResultSet object, and implicitly closes any current ResultSet object(s) obtained with the method getResultSet.
    ResultSet getGeneratedKeys() Retrieves any auto-generated keys created as a result of executing this Statement object. If this Statement object did not generate any keys, an empty ResultSet object is returned.
  • To test for multiple ResultSet you can use this:
    //  There are no more results when the following is true:
    //  stmt is a Statement object
    ((stmt.getMoreResults() == false) && (stmt.getUpdateCount() == -1))

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.