Executing SQL Queries

The most commonly used SQL statement is the SELECT statement. We can execute SELECT statements using either a Statement or a PreparedStatement object. Both Statement and PreparedStatement have a method, executeQuery(), that executes an SQL SELECT command and returns a ResultSet containing the results of the query in the form of a table. Consider this query using Statement:

Statement stmt = new conn.getStatement();
ResultSet rs = stmt.executeQuery(
   "SELECT ENAME, JOB, SAL FROM EMP");

The ResultSet that this example returns contains three columns, ENAME, JOB, and SAL, as specified in the select list. Assuming the database hasn't been altered since Oracle was installed, it will contain 14 rows.

Navigating a ResultSet

We access ...

Get Java™ Oracle® Database Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.