Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import org.apache.commons.beanutils.*;
  2.  
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7.  
  8. public class ResultSetIteratorAttempt {
  9.  
  10.  
  11. public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  12.  
  13. Connection conn = null;
  14. Statement stmt = null;
  15.  
  16. Class.forName("com.mysql.jdbc.Driver");
  17.  
  18. conn = DriverManager.getConnection("jdbc:mysql://localhost/sample","java","");
  19.  
  20. stmt = conn.createStatement();
  21.  
  22. ResultSet rs = stmt.executeQuery("SELECT Id as NM1, Name as NM2 FROM DayOfWeek;");
  23.  
  24. ArrayList results = new ArrayList(); // To hold copied list
  25.  
  26. ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);
  27. rsdc.setUseColumnLabel(true);
  28.  
  29. DynaProperty[] properties = rsdc.getDynaProperties();
  30. String propertyName = "";
  31. for(int i=0; i<properties.length; ++i) {
  32. propertyName = properties[i].getName();
  33. System.out.println(String.format("Property #%d is %s", i, propertyName));
  34. }
  35.  
  36. Iterator rows = rsdc.iterator();
  37. DynaBean row;
  38. while (rows.hasNext()) {
  39. row = (DynaBean) rows.next();
  40. System.out.println(String.format("Row is %s", row.get(propertyName)));
  41. }
  42.  
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement