1 53 54 106 107 package com.Yasna.forum.database; 108 109 import java.util.Iterator ; 110 import java.util.ListIterator ; 111 import java.util.ArrayList ; 112 import java.sql.*; 113 import com.Yasna.forum.*; 114 115 119 public class DbUserIterator implements Iterator , ListIterator { 120 121 122 private static final String ALL_USERS = 123 "SELECT userID from yazdUser"; 124 125 private int currentIndex = -1; 126 private int [] users; 127 128 private ProfileManager profileManager; 129 130 protected DbUserIterator(ProfileManager profileManager) { 131 this.profileManager = profileManager; 132 ArrayList tempUsers = new ArrayList (); 135 Connection con = null; 136 PreparedStatement pstmt = null; 137 try { 138 con = DbConnectionManager.getConnection(); 139 pstmt = con.prepareStatement(ALL_USERS); 140 ResultSet rs = pstmt.executeQuery(); 141 while (rs.next()) { 142 tempUsers.add(new Integer (rs.getInt("userID"))); 143 } 144 } 145 catch( SQLException sqle ) { 146 System.err.println("Error in DbUserIterator:constructor()-" + sqle); 147 } 148 finally { 149 try { pstmt.close(); } 150 catch (Exception e) { e.printStackTrace(); } 151 try { con.close(); } 152 catch (Exception e) { e.printStackTrace(); } 153 } 154 users = new int[tempUsers.size()]; 156 for (int i=0; i<users.length; i++) { 157 users[i] = ((Integer )tempUsers.get(i)).intValue(); 158 } 159 } 160 161 protected DbUserIterator(ProfileManager profileManager, int startIndex, 162 int numResults) 163 { 164 this.profileManager = profileManager; 165 166 int[] tempUsers = new int[numResults]; 167 int userCount = 0; 171 172 Connection con = null; 173 PreparedStatement pstmt = null; 174 175 try { 176 con = DbConnectionManager.getConnection(); 177 pstmt = con.prepareStatement(ALL_USERS); 178 ResultSet rs = pstmt.executeQuery(); 179 for (int i=0; i<startIndex; i++) { 181 rs.next(); 182 } 183 for (int i=0; i<numResults; i++) { 185 if (rs.next()) { 186 tempUsers[userCount] = rs.getInt("userID"); 187 userCount++; 188 } 189 else { 190 break; 191 } 192 } 193 } 194 catch( SQLException sqle ) { 195 System.err.println("Error in DbUserIterator:constructor()-" + sqle); 196 } 197 finally { 198 try { pstmt.close(); } 199 catch (Exception e) { e.printStackTrace(); } 200 try { con.close(); } 201 catch (Exception e) { e.printStackTrace(); } 202 } 203 users = new int[userCount]; 204 for (int i=0; i<userCount; i++) { 205 users[i] = tempUsers[i]; 206 } 207 } 208 209 212 public boolean hasNext() { 213 return (currentIndex+1 < users.length); 214 } 215 216 219 public boolean hasPrevious() { 220 return (currentIndex > 0); 221 } 222 223 229 public Object next() throws java.util.NoSuchElementException { 230 User user = null; 231 currentIndex++; 232 if (currentIndex >= users.length) { 233 throw new java.util.NoSuchElementException (); 234 } 235 try { 236 user = profileManager.getUser(users[currentIndex]); 237 } 238 catch (UserNotFoundException gnfe) { 239 gnfe.printStackTrace(); 240 } 241 return user; 242 } 243 244 250 public void add(Object o) throws UnsupportedOperationException { 251 throw new UnsupportedOperationException (); 252 } 253 254 260 public void remove() { 261 throw new UnsupportedOperationException (); 262 } 263 264 270 public void set(Object o) throws UnsupportedOperationException { 271 throw new UnsupportedOperationException (); 272 } 273 274 277 public int nextIndex() { 278 return currentIndex+1; 279 } 280 281 284 public Object previous() throws java.util.NoSuchElementException { 285 User user = null; 286 currentIndex--; 287 if (currentIndex < 0) { 288 currentIndex++; 289 throw new java.util.NoSuchElementException (); 290 } 291 try { 292 user = profileManager.getUser(users[currentIndex]); 293 } 294 catch (UserNotFoundException gnfe) { 295 System.err.println(gnfe); 296 } 297 return user; 298 } 299 300 303 public int previousIndex() { 304 return currentIndex-1; 305 } 306 } 307 | Popular Tags |