Select records using Prepared Statement

To select records using Prepared Statement in java we use

“A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.”

Steps to Execute Select SQL Query using Prepared Statement

1. Get a connection Object

2. Create a prepareStatement object

3. Add parameters (if required)
As you can see we have ? in the query. So thats a parameter and we need to pass values for that parameter.

The first parameter in the setInt method is the parameterIndex and the 2nd param is the parameter value for the query.

Please note: ParameterIndex starts from 1.

4. execute the query using executeQuery.

Full Example : Select records using Prepared Statement in Java



1. PreparedStatement executeQuery()
2. ResultSet