1 package com.Yasna.forum.database; 2 3 4 import com.Yasna.forum.ThreadType; 5 import com.Yasna.forum.Exceptions.ThreadTypeNotFoundException; 6 7 import java.util.Iterator ; 8 import java.util.ListIterator ; 9 import java.util.ArrayList ; 10 import java.sql.Connection ; 11 import java.sql.PreparedStatement ; 12 import java.sql.ResultSet ; 13 import java.sql.SQLException ; 14 15 67 public class DbThreadTypeIterator implements Iterator , ListIterator { 68 private static final String ALL_TYPES = "SELECT typeID from yazdThreadType"; 69 70 private int currentIndex = -1; 71 private int [] types; 72 private DbForumFactory factory; 73 protected DbThreadTypeIterator(DbForumFactory factory){ 74 this.factory=factory; 75 ArrayList tempArr = new ArrayList (); 76 Connection con = null; 77 PreparedStatement pstmt = null; 78 try { 79 con = DbConnectionManager.getConnection(); 80 pstmt = con.prepareStatement(ALL_TYPES); 81 ResultSet rs = pstmt.executeQuery(); 82 while (rs.next()) { 83 tempArr.add(new Integer (rs.getInt("typeID"))); 84 } 85 } 86 catch( SQLException sqle ) { 87 System.err.println("Error in DbThreadTypeIterator:constructor()-" + sqle); 88 } 89 finally { 90 try { pstmt.close(); } 91 catch (Exception e) { e.printStackTrace(); } 92 try { con.close(); } 93 catch (Exception e) { e.printStackTrace(); } 94 } 95 types = new int[tempArr.size()]; 96 for (int i=0; i<types.length; i++) { 97 types[i] = ((Integer )tempArr.get(i)).intValue(); 98 } 99 100 } 101 102 public boolean hasNext() { 103 return (currentIndex+1 < types.length); 104 } 105 106 109 public Object next() throws java.util.NoSuchElementException { 110 currentIndex++; 111 if (currentIndex >= types.length) { 112 throw new java.util.NoSuchElementException (); 113 } 114 return factory.getThreadType(types[currentIndex]); 115 } 116 117 123 public void remove() { 124 throw new UnsupportedOperationException (); 125 } 126 127 130 public boolean hasPrevious() { 131 return (currentIndex > 0); 132 } 133 134 140 public void add(Object o) throws UnsupportedOperationException { 141 throw new UnsupportedOperationException (); 142 } 143 144 150 public void set(Object o) throws UnsupportedOperationException { 151 throw new UnsupportedOperationException (); 152 } 153 154 157 public int nextIndex() { 158 return currentIndex+1; 159 } 160 161 164 public Object previous() throws java.util.NoSuchElementException { 165 currentIndex--; 166 if (currentIndex < 0) { 167 currentIndex++; 168 throw new java.util.NoSuchElementException (); 169 } 170 return factory.getThreadType(types[currentIndex]); 171 } 172 173 176 public int previousIndex() { 177 return currentIndex-1; 178 } 179 180 } 181 | Popular Tags |