java.sql.ResultSet is an interface that can hold the records from the database.
Types of ResultSet in JDBC
ResultSet in JDBC can be of 3 types and can be defined while creation of the Statement
1. TYPE_FORWARD_ONLY – Default
2. TYPE_SCROLL_SENSITIVE – Created using ResultSet.TYPE_SCROLL_SENSITIVE
3. TYPE_SCROLL_INSENSITIVE – Created using ResultSet.TYPE_SCROLL_INSENSITIVE
How to create ResultSet
1 2 3 4 5 6 7 8 9 10 11 |
//Simple ResulsetSet - Default - TYPE_FORWARD_ONLY Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(selectSql); //TYPE_SCROLL_SENSITIVE Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet resultSet = statement.executeQuery(selectSql); //TYPE_SCROLL_INSENSITIVE Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet resultSet = statement.executeQuery(selectSql); |
Get More details – Here
Use of ResultSet in JDBC
1. To Select and retrieve data from database
Please go through the below links for sample codes
a. Select records using Prepared Statement
b. Select records using Statement
2. Update/Insert records that were retrived from database.
For updating or inserting data using a resultSet, you need to have the ResulSet as UPDATABLE
This is how we can make a ResultSet updatable
1 |
statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE); |
Please go through the below links for sample codes
a. Insert Record using ResultSet
b. Update Record using ResultSet