1 40 package org.dspace.storage.rdbms; 41 42 import java.sql.ResultSet ; 43 import java.sql.SQLException ; 44 import java.sql.Statement ; 45 import java.util.ArrayList ; 46 import java.util.List ; 47 48 54 public class TableRowIterator 55 { 56 59 private ResultSet results; 60 61 64 private Statement statemt = null; 65 66 69 private String table; 70 71 74 private boolean hasNext = true; 75 76 79 private boolean hasAdvanced = false; 80 81 87 TableRowIterator(ResultSet results) 88 { 89 this(results, null); 90 statemt = null; 91 } 92 93 101 TableRowIterator(ResultSet results, String table) 102 { 103 this.results = results; 104 this.table = table; 105 statemt = null; 106 } 107 108 111 public void finalize() 112 { 113 close(); 114 } 115 116 124 public void setStatement(Statement st) 125 { 126 statemt = st; 127 } 128 129 137 public TableRow next() throws SQLException 138 { 139 if (results == null) 140 { 141 return null; 142 } 143 144 if (!hasNext()) 145 { 146 return null; 147 } 148 149 hasAdvanced = false; 150 151 return DatabaseManager.process(results, table); 152 } 153 154 161 public boolean hasNext() throws SQLException 162 { 163 if (results == null) 164 { 165 return false; 166 } 167 168 if (hasAdvanced) 169 { 170 return hasNext; 171 } 172 173 hasAdvanced = true; 174 hasNext = results.next(); 175 176 if (!hasNext) 178 { 179 close(); 180 } 181 182 return hasNext; 183 } 184 185 195 public List toList() throws SQLException 196 { 197 List resultsList = new ArrayList (); 198 199 while (hasNext()) 200 { 201 resultsList.add(next()); 202 } 203 204 this.close(); 206 207 return resultsList; 208 } 209 210 213 public void close() 214 { 215 try 216 { 217 results.close(); 218 if (results != null) 219 results.close(); 220 } 221 catch (SQLException sqle) 222 { 223 } 224 225 try 227 { 228 if (statemt != null) 229 statemt.close(); 230 statemt = null; 231 } 232 catch (SQLException sqle) 233 { 234 } 235 } 236 } 237 | Popular Tags |