Advanced   Java   Services Statement Back Next Up Home


Von der Connection zum Statement

Statement ist ein Interface, das von den verschiedenen Treibern durch entsprechende Klassen implementiert wird (siehe nächste Seite). Mit einem JavaStatementObjekt kann man SQL-Statements an die Datenbank abschicken. Hat man ein Connection-Objekt, also eine aktive Datenbankverbindung, so führen (seit Java 1.4) drei Methoden zu einem Statementobjekt:

Statement connection.createStatement();
//Creates a Statement object for sending SQL statements to the database.
Statement connection.createStatement(int resultSetType, int resultSetConcurrency);
//Creates a Statement object that will generate ResultSet objects with the given type and concurrency.
Statement connection.createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability);
//Creates a Statement object that will generate ResultSet objects with the given type, concurrency, and holdability.



Parameter für resultSetType

Parameter für resultSetConcurrency

Parameter für resultSetHoldability

Die Parameter beeinflußen die Fähigkeiten eines ResultSetObjektes.





Java-Statements und SQL-Statements

Ein Java-Statement dient dazu, SQL-Statements auf die Datenbank abzusetzen. Dabei werden Javaseitig nur zwei Arten von SQL-Statements unterschieden. Entweder handelt es sich um ein SELECT-Statement oder um kein SELECT-Statement.





SELECT und executeQuery()

Ein SELECT-Statement liefert einen Auszug aus einer Tabelle (oder aus mehreren Tabellen oder auch eine ganze Tabelle). Ein SELECT-Statement wird für gewöhnlich mit der Methode public ResultSet executeQuery(String sql) abgeschickt und liefert den Tabellenauszug in Form eines Objekts vom Typ ResultSet zurück. Das Interface ResultSet vereinbart mittlerweile mehr als 150 (!) Methoden um auf ein geliefertes ResultSet lesend oder schreibend zuzugreifen. Dabei können nicht alle Methoden auf alle ResultSets angewendet werden. Es gibt sozusagen Resultsets unterschiedlicher Qualität. Mehr dazu auf der ResultSet Seite.




Nicht SELECT und executeUpdate()

Alle nicht SELECT-Statements kann man mit der Methode public int executeUpdate(String sql) abschicken. Mit executeUpdate() werden sowohl CREATE-Statements als auch Statements mit INSERT, DELETE, ALTER, RENAME oder UPDATE usw. angeschickt. Der Returnwert von executeUpdate() ist bei INSERT, UPDATE oder DELETE die Anzahl der betroffenen Zeilen, bei CREATE oder DROP ist er 0.

Alle anderen execute() und executeXXX() Methoden sind für komplexe SQL-Statements zuständig.


Auszug aus der API

Interface java.sql.Statement
Konstanten (ab Java 1.4)
TypName der Konstanten
static intCLOSE_ALL_RESULTS
The constant indicating that all ResultSet objects that have previously been kept open should be closed when calling getMoreResults.
static intCLOSE_CURRENT_RESULT
The constant indicating that the current ResultSet object should be closed when calling getMoreResults.
static intCLOSE_CURRENT_RESULT
The constant indicating that the current ResultSet object should be closed when calling getMoreResults.
static intEXECUTE_FAILED
The constant indicating that an error occured while executing a batch statement.
static intKEEP_CURRENT_RESULT
The constant indicating that the current ResultSet object should not be closed when calling getMoreResults.
static intNO_GENERATED_KEYS
The constant indicating that generated keys should not be made available for retrieval.
static intRETURN_GENERATED_KEYS
The constant indicating that generated keys should be made available for retrieval.
static intSUCCESS_NO_INFO
The constant indicating that a batch statement executed successfully but that no count of the number of rows it affected is available.
Methoden (Auszug)
ReturntypName der Methode
booleanexecute(String sql)
Executes the given SQL statement, which may return multiple results.
booleanexecute(String sql, int autoGeneratedKeys)
Executes the given SQL statement, which may return multiple results, and signals the driver that any auto-generated keys should be made available for retrieval.
booleanexecute(String sql, int[] columnIndexes)
Executes the given SQL statement, which may return multiple results, and signals the driver that the auto-generated keys indicated in the given array should be made available for retrieval.
booleanexecute(String sql, String[] columnNames)
Executes the given SQL statement, which may return multiple results, and signals the driver that the auto-generated keys indicated in the given array should be made available for retrieval.
ResultSetexecuteQuery(String sql)
Executes the given SQL statement, which returns a single ResultSet object.
intexecuteUpdate(String sql)
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
intexecuteUpdate(String sql, int autoGeneratedKeys)
Executes the given SQL statement and signals the driver with the given flag about whether the auto-generated keys produced by this Statement object should be made available for retrieval.
intexecuteUpdate(String sql, int[] columnIndexes)
Executes the given SQL statement and signals the driver that the auto-generated keys indicated in the given array should be made available for retrieval.
intexecuteUpdate(String sql, String[] columnNames)
Executes the given SQL statement and signals the driver that the auto-generated keys indicated in the given array should be made available for retrieval.
booleangetMoreResults()
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.
booleangetMoreResults(int current)
Moves to this Statement object's next result, deals with any current ResultSet object(s) according to the instructions specified by the given flag, and returns true if the next result is a ResultSet object.
ResultSetgetResultSet()
Retrieves the current result as a ResultSet object.
intgetResultSetConcurrency()
Retrieves the result set concurrency for ResultSet objects generated by this Statement object (ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE) .
intgetResultSetHoldability()
Retrieves the result set holdability for ResultSet objects generated by this Statement object (ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
intgetResultSetType()
Retrieves the result set type for ResultSet objects generated by this Statement object (ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIV).
intgetUpdateCount()
Retrieves the current result as an update count; if the result is a ResultSet object or there are no more results, -1 is returned.


Abschicken von SQL-Statements mit den execute-Methoden





Statement stmt = connection.createStatement();
String createCoffees = "create table COFFEES( COF_NAME varchar(32), SUP_ID integer, "
                     + "PRICE float, SALES integer, TOTAL integer )" ;
int erg = stmt.executeUpdate(createCoffees);
// erg ist 0

oder

Statement stmt = connection.createStatement();
String insert = "insert into COFFEES values( 'Espresso' , 99, 4.59, 0, 0 )" ;
int erg = stmt.executeUpdate(insert);
// erg ist 0

oder

Statement stmt = connection.createStatement();
String query = "select * from MYTABLE" ;
ResultSet rs = stmt.executeQuery(query);

oder

Statement stmt = connection.createStatement();
String query = "select COF_NAME , SUP_ID from COFFEES" ;
ResultSet rs = stmt.executeQuery(query);

Alle Statements werden datenbankneutral abgeschickt. Datenbankabhängige Endemarkierungen wie etwa ein Semikolon werden nicht in den String mit aufgenommen.





Valid XHTML 1.0 Strict top Back Next Up Home