1 53 54 package com.Yasna.forum.database; 55 56 import java.util.*; 57 import java.sql.*; 59 import com.Yasna.forum.*; 60 61 64 public class DbGroupIterator implements Iterator, ListIterator { 65 66 67 private static final String ALL_GROUPS = "SELECT groupID from yazdGroup"; 68 69 private int currentIndex = -1; 70 private int [] groups; 71 72 private ProfileManager profileManager; 73 74 protected DbGroupIterator(ProfileManager profileManager) { 75 this.profileManager = profileManager; 76 ArrayList tempGroups = new ArrayList(); 79 Connection con = null; 80 PreparedStatement pstmt = null; 81 try { 82 con = DbConnectionManager.getConnection(); 83 pstmt = con.prepareStatement(ALL_GROUPS); 84 ResultSet rs = pstmt.executeQuery(); 85 while (rs.next()) { 86 tempGroups.add(new Integer (rs.getInt("groupID"))); 87 } 88 } 89 catch( SQLException sqle ) { 90 System.err.println("Error in DbGroupIterator:constructor()-" + sqle); 91 } 92 finally { 93 try { pstmt.close(); } 94 catch (Exception e) { e.printStackTrace(); } 95 try { con.close(); } 96 catch (Exception e) { e.printStackTrace(); } 97 } 98 groups = new int[tempGroups.size()]; 99 for (int i=0; i<groups.length; i++) { 100 groups[i] = ((Integer )tempGroups.get(i)).intValue(); 101 } 102 } 103 104 protected DbGroupIterator(ProfileManager profileManager, int startIndex, 105 int numResults) 106 { 107 this.profileManager = profileManager; 108 109 int[] tempResults = new int[numResults]; 110 int resultCount = 0; 114 115 Connection con = null; 116 PreparedStatement pstmt = null; 117 118 try { 119 con = DbConnectionManager.getConnection(); 120 pstmt = con.prepareStatement(ALL_GROUPS); 121 ResultSet rs = pstmt.executeQuery(); 122 for (int i=0; i<startIndex; i++) { 124 rs.next(); 125 } 126 for (int i=0; i<numResults; i++) { 128 if (rs.next()) { 129 tempResults[resultCount] = rs.getInt("groupID"); 130 resultCount++; 131 } 132 else { 133 break; 134 } 135 } 136 } 137 catch( SQLException sqle ) { 138 System.err.println("Error in DbGroupIterator:constructor()-" + sqle); 139 } 140 finally { 141 try { pstmt.close(); } 142 catch (Exception e) { e.printStackTrace(); } 143 try { con.close(); } 144 catch (Exception e) { e.printStackTrace(); } 145 } 146 groups = new int[resultCount]; 147 for (int i=0; i<resultCount; i++) { 148 groups[i] = tempResults[i]; 149 } 150 } 151 152 155 public boolean hasNext() { 156 return (currentIndex+1 < groups.length); 157 } 158 159 162 public Object next() throws java.util.NoSuchElementException { 163 Group group = null; 164 currentIndex++; 165 if (currentIndex >= groups.length) { 166 throw new java.util.NoSuchElementException (); 167 } 168 try { 169 group = profileManager.getGroup(groups[currentIndex]); 170 } 171 catch (GroupNotFoundException gnfe) { 172 System.err.println(gnfe); 173 } 174 return group; 175 } 176 177 183 public void remove() { 184 throw new UnsupportedOperationException (); 185 } 186 187 190 public boolean hasPrevious() { 191 return (currentIndex > 0); 192 } 193 194 200 public void add(Object o) throws UnsupportedOperationException { 201 throw new UnsupportedOperationException (); 202 } 203 204 210 public void set(Object o) throws UnsupportedOperationException { 211 throw new UnsupportedOperationException (); 212 } 213 214 217 public int nextIndex() { 218 return currentIndex+1; 219 } 220 221 224 public Object previous() throws java.util.NoSuchElementException { 225 Group group = null; 226 currentIndex--; 227 if (currentIndex < 0) { 228 currentIndex++; 229 throw new java.util.NoSuchElementException (); 230 } 231 try { 232 group = profileManager.getGroup(groups[currentIndex]); 233 } 234 catch (GroupNotFoundException gnfe) { 235 System.err.println(gnfe); 236 } 237 return group; 238 } 239 240 243 public int previousIndex() { 244 return currentIndex-1; 245 } 246 } 247 248 | Popular Tags |