1 53 54 package com.Yasna.forum.database; 55 56 import com.Yasna.forum.*; 57 import com.Yasna.util.*; 58 import java.util.*; 59 import java.sql.*; 61 62 65 public class DbCategoryIterator implements Iterator, ListIterator { 66 67 68 private static final String GET_CATEGORIES = "SELECT categoryID FROM yazdCategory ORDER BY catorder DESC"; 69 70 private ForumFactory factory; 71 private int [] categories; 72 int currentIndex = -1; 74 75 protected DbCategoryIterator(ForumFactory factory) { 76 this.factory = factory; 77 ArrayList allCategories = new ArrayList(); 78 Connection con = null; 79 PreparedStatement pstmt = null; 80 try { 81 con = DbConnectionManager.getConnection(); 82 pstmt = con.prepareStatement(GET_CATEGORIES); 83 ResultSet rs = pstmt.executeQuery(); 84 while (rs.next()) { 85 allCategories.add(new Integer (rs.getInt("categoryID"))); 86 } 87 } 88 catch( SQLException sqle ) { 89 System.err.println("Error in DbCategoryFactoryIterator:constructor()-" + sqle); 90 } 91 finally { 92 try { pstmt.close(); } 93 catch (Exception e) { e.printStackTrace(); } 94 try { con.close(); } 95 catch (Exception e) { e.printStackTrace(); } 96 } 97 categories = new int[allCategories.size()]; 99 for (int i=0; i<categories.length; i++) { 100 categories[i] = ((Integer )allCategories.get(i)).intValue(); 101 } 102 } 103 104 107 public boolean hasNext() { 108 return (currentIndex+1 < categories.length); 109 } 110 111 114 public Object next() throws java.util.NoSuchElementException { 115 Category category = null; 116 currentIndex++; 117 if (currentIndex >= categories.length) { 118 throw new java.util.NoSuchElementException (); 119 } 120 try { 121 category = factory.getCategory(categories[currentIndex]); 122 } 123 catch (Exception e) { 124 e.printStackTrace(); 125 } 126 return category; 127 } 128 129 130 public void remove() { 131 throw new UnsupportedOperationException (); 132 } 133 134 public void add(Object o) throws UnsupportedOperationException { 135 throw new UnsupportedOperationException (); 136 } 137 138 public boolean hasPrevious() { 139 return (currentIndex > 0); 140 } 141 142 public int nextIndex() { 143 return currentIndex+1; 144 } 145 146 public Object previous() throws java.util.NoSuchElementException { 147 Category category = null; 148 currentIndex--; 149 if (currentIndex < 0) { 150 currentIndex++; 151 throw new java.util.NoSuchElementException (); 152 } 153 try { 154 category = factory.getCategory(categories[currentIndex]); 155 } 156 catch (Exception e) { 157 e.printStackTrace(); 158 } 159 return category; 160 } 161 162 public int previousIndex() { 163 return currentIndex-1; 164 } 165 166 public void set(Object o) throws UnsupportedOperationException { 167 throw new UnsupportedOperationException (); 168 } 169 } 170 | Popular Tags |