Select Records JDBC Example

Select Records JDBC Example

Select Records JDBC Example: To retrieve records from Employee table in Oracle Database.

By the time this program is executed, see some records exist in Employee table.

Example on Select Records JDBC Example

Select Records JDBC Example

a) Statement interface includes two methods (discussed in Learning JDBC Programming).

1. executeQuery(String) used for select statements that returns an object ResultSet interface.

2. executeUpdate(String) used for any statement other than select statement that returns a primitive data type int.


Column number parameters in the above statement can be replaced with column names as follows:


c) For printing purpose we can also write:




d) The next() method of ResultSet interface returns true if records exist. If all the records are over, the method returns false and the loop terminates.

The difference between getInt(1) and getString(1) is the first one returns the value of the column as an integer which can be used in the program directly for mathematical calculations and the second method returns a string. This string is useful just to print the value but cannot be used in the program for arithmetic operations.

getInt(), getString() and getDouble() methods of ResultSet interface returns an integer, a string and double value. Some more get() methods exist and are discussed in JDBC Programming Basic Steps

ResultSet Cursor Movement or how next() method executes

Let us see how the while loop returns all the records as it is required almost in each JDBC program.

JDBC maintains a cursor over the records. By default the cursor lies before the first record where actually no record exists. The next() method moves the cursor to the first record. Now the cursor is pointing to the first record. I call the first record as the current record (the record which the cursor points). The getXXX() methods read the fields of the current record, that is first record.


After getXXX() methods read the current record data, the iteration continuous. Now the next() method moves the cursor over to the 2nd record. If the cursor points a record, the next() method returns a boolean value of true and the execution goes into the loop body. The getXXX() methods read the 2nd record data. Now the current record is 2nd record. That is, getXXX() methods read always the current record data. When the records get exhausted, the cursor comes after the last record where there is no record (other way, there is no current record). Now the next() method returns false and the loop terminates. It is similar to the Enumeration iteration.

Note 1: Before going into the program, it is advised to go through JDBC Programming Basic Steps where the meaning of Connection, Statement etc. are discussed.

Note 2: To know how to use try-catch-finally with the above code, example program is available at JDBC Example – Create Table.

One thought on “Select Records JDBC Example

Leave a Reply

Your email address will not be published. Required fields are marked *