Java BeanUtils - ResultSetDynaClass



Description

The ResultSet can be wrapped in the DynaBeans by using the ResultSetDynaClass which renders the results of SQL query as series of DynaBeans. The most commonly used collection is java.sql.ResultSet which is returned when JDBC driver uses SQL SELECT statement. The each row of result set can be made visible by using the Commons BeanUtils package.

You can make use of the ResultSetDynaClass by using the DynaBean interface as shown in the below code snippet:

Connection conn = ...;
Statement stmt = conn.createStatement();
ResultSet res_set = stmt.executeQuery("select first_name, last_name from student");
Iterator rows = (new ResultSetDynaClass(res_set)).iterator();
while (rows.hasNext()) {
   DynaBean row = (DynaBean) rows.next();
   System.out.println("First Name is:" + row.get("first_name") + " and Last Name is:" + row.get("last_name"));
}
rs.close();
stmt.close();
Advertisements