Enjoying this Java Tutorial?
Subscribe to Ram N Java for regular, beginner-friendly programming tutorials and real-world coding demos!
SUBSCRIBE NOWWhat is a JDBCRowSet?
In standard Java Database Connectivity (JDBC), working with standard ResultSet objects often requires manually opening connections, managing statements, and tracking resource life cycles. A JDBCRowSet wraps a standard ResultSet into a JavaBean component, making database handling flexible, easy to configure, and scrollable by default.
Key Steps to Create and Use JDBCRowSet
To implement a JdbcRowSet in your Java application, you follow these straightforward steps:
- Obtain the RowSetProvider: Call
RowSetProvider.newFactory()to obtain an instance ofRowSetFactory. - Create JdbcRowSet: Use
rowSetFactory.createJdbcRowSet()to get your row set instance. - Configure Connection Parameters: Set the database connection URL, username, and password using dedicated setter methods.
- Define Query & Parameters: Set the SQL statement via
setCommand()and bind query placeholders with setters likesetString(). - Execute and Process: Trigger
execute()to load the records, iterate through the row set, and access column values smoothly.
Complete Java Code Example
import java.sql.SQLException;
import java.util.Scanner;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
public class JdbcRowSetDemo {
public static void main(String[] args) {
JdbcRowSetDemo demo = new JdbcRowSetDemo();
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter Country Code (e.g., IND, USA): ");
String countryCode = scanner.next();
demo.getCityInformation(countryCode);
} catch (Exception e) {
e.printStackTrace();
}
}
public void getCityInformation(String countryCode) {
try {
// 1. Create RowSetFactory
RowSetFactory factory = RowSetProvider.newFactory();
// 2. Create JdbcRowSet instance
try (JdbcRowSet jdbcRowSet = factory.createJdbcRowSet()) {
// 3. Set connection properties
jdbcRowSet.setUrl("jdbc:mysql://localhost:3306/world");
jdbcRowSet.setUsername("root");
jdbcRowSet.setPassword("password");
// 4. Set SQL query command and bind parameters
jdbcRowSet.setCommand("SELECT ID, Name, CountryCode, District, Population FROM city WHERE CountryCode = ?");
jdbcRowSet.setString(1, countryCode);
// 5. Execute query
jdbcRowSet.execute();
// 6. Iterate through the records
while (jdbcRowSet.next()) {
System.out.println("ID: " + jdbcRowSet.getInt("ID"));
System.out.println("Name: " + jdbcRowSet.getString("Name"));
System.out.println("CountryCode: " + jdbcRowSet.getString("CountryCode"));
System.out.println("District: " + jdbcRowSet.getString("District"));
System.out.println("Population: " + jdbcRowSet.getInt("Population"));
System.out.println("------------------------------------------");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Watch More Related JDBC Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=szmvtqQGZLQ
Click the below Image to Enlarge
![]() |
| JDBCRowSet |
![]() |
| JDBCRowSet |
![]() |
| JDBCRowSet |



Hi,
ReplyDeleteI'm wondering, what would be the best use-cases for using this type?
Many thx's.