Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.commons.beanutils.*;
- import java.lang.reflect.InvocationTargetException;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class ResultSetIteratorAttempt {
- public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
- Connection conn = null;
- Statement stmt = null;
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/sample","java","");
- stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT Id as NM1, Name as NM2 FROM DayOfWeek;");
- ArrayList results = new ArrayList(); // To hold copied list
- ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);
- rsdc.setUseColumnLabel(true);
- DynaProperty[] properties = rsdc.getDynaProperties();
- String propertyName = "";
- for(int i=0; i<properties.length; ++i) {
- propertyName = properties[i].getName();
- System.out.println(String.format("Property #%d is %s", i, propertyName));
- }
- Iterator rows = rsdc.iterator();
- DynaBean row;
- while (rows.hasNext()) {
- row = (DynaBean) rows.next();
- System.out.println(String.format("Row is %s", row.get(propertyName)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement