1 18 package org.apache.beehive.controls.system.jdbc; 19 20 import org.apache.beehive.controls.api.ControlException; 21 import org.apache.beehive.controls.api.context.ControlBeanContext; 22 23 import java.lang.reflect.Method ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.util.Calendar ; 27 import java.util.NoSuchElementException ; 28 29 32 public class ResultSetIterator implements java.util.Iterator { 33 34 private final Class _returnClass; 35 private final ResultSet _rs; 36 private final RowMapper _rowMapper; 37 38 private boolean _primed = false; 39 40 47 ResultSetIterator(ControlBeanContext context, Method method, ResultSet rs, Calendar cal) { 48 _rs = rs; 49 50 JdbcControl.SQL methodSQL = (JdbcControl.SQL) context.getMethodPropertySet(method, JdbcControl.SQL.class); 51 _returnClass = methodSQL.iteratorElementType(); 52 53 if (_returnClass == null) { 54 throw new ControlException("Invalid return class declared for Iterator:" + _returnClass.getName()); 55 } 56 57 _rowMapper = RowMapperFactory.getRowMapper(rs, _returnClass, cal); 58 } 59 60 64 public boolean hasNext() { 65 if (_primed) { 66 return true; 67 } 68 69 try { 70 _primed = _rs.next(); 71 } catch (SQLException sqle) { 72 return false; 73 } 74 return _primed; 75 } 76 77 81 public Object next() { 82 try { 83 if (!_primed) { 84 _primed = _rs.next(); 85 if (!_primed) { 86 throw new NoSuchElementException (); 87 } 88 } 89 _primed = false; 91 return _rowMapper.mapRowToReturnType(); 92 } catch (SQLException e) { 93 NoSuchElementException xNoSuch = new NoSuchElementException ("ResultSet exception: " + e); 96 xNoSuch.initCause(e); 97 throw xNoSuch; 98 } 99 } 100 101 104 public void remove() { 105 throw new UnsupportedOperationException ("remove not supported"); 106 } 107 } 108 109 | Popular Tags |