1 15 package org.apache.tapestry.contrib.table.model.sql; 16 17 import java.sql.ResultSet ; 18 import java.sql.SQLException ; 19 import java.util.Iterator ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 28 public class ResultSetIterator implements Iterator 29 { 30 private static final Log LOG = LogFactory.getLog(ResultSetIterator.class); 31 32 private ResultSet m_objResultSet; 33 private boolean m_bFetched; 34 private boolean m_bAvailable; 35 36 public ResultSetIterator(ResultSet objResultSet) 37 { 38 m_objResultSet = objResultSet; 39 m_bFetched = false; 40 } 41 42 45 public synchronized boolean hasNext() 46 { 47 if (getResultSet() == null) return false; 48 49 if (!m_bFetched) 50 { 51 m_bFetched = true; 52 53 try 54 { 55 m_bAvailable = !getResultSet().isLast(); 56 } 57 catch (SQLException e) 58 { 59 LOG.warn( 60 "SQLException while testing for end of the ResultSet", 61 e); 62 m_bAvailable = false; 63 } 64 65 if (!m_bAvailable) 66 notifyEnd(); 67 } 68 69 return m_bAvailable; 70 } 71 72 75 public synchronized Object next() 76 { 77 ResultSet objResultSet = getResultSet(); 78 79 try 80 { 81 if (!objResultSet.next()) 82 return null; 83 } 84 catch (SQLException e) 85 { 86 LOG.warn("SQLException while iterating over the ResultSet", e); 87 return null; 88 } 89 90 m_bFetched = false; 91 return objResultSet; 92 } 93 94 97 public void remove() 98 { 99 try 100 { 101 getResultSet().deleteRow(); 102 } 103 catch (SQLException e) 104 { 105 LOG.error("Cannot delete record", e); 106 } 107 } 108 109 113 public ResultSet getResultSet() 114 { 115 return m_objResultSet; 116 } 117 118 protected void notifyEnd() 119 { 120 } 121 122 } 123 | Popular Tags |