KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > persist > ResultSetIterator


1 // $Id: ResultSetIterator.java 12 2007-08-29 05:23:13Z jcamaia $
2

3 package net.sf.persist;
4
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 /**
10  * Iterator backed by a ResultSet.
11  */

12 public final class ResultSetIterator implements Iterator JavaDoc {
13
14     public static final int TYPE_OBJECT = 1;
15     public static final int TYPE_MAP = 2;
16
17     private final ResultSet JavaDoc resultSet;
18     private final Persist persist;
19     private final Class JavaDoc objectClass;
20     private final int type;
21     private boolean hasNext = false;
22
23     public ResultSetIterator(final Persist persist, final Class JavaDoc objectClass, final ResultSet JavaDoc resultSet, final int type) {
24
25         if (type != TYPE_OBJECT && type != TYPE_MAP) {
26             throw new RuntimeSQLException("Invalid ResultSetIterator type: " + type);
27         }
28
29         this.persist = persist;
30         this.objectClass = objectClass;
31         this.resultSet = resultSet;
32         this.type = type;
33
34         try {
35             hasNext = resultSet.next();
36         } catch (SQLException JavaDoc e) {
37             throw new RuntimeSQLException(e);
38         }
39     }
40
41     public boolean hasNext() {
42         return hasNext;
43     }
44
45     public Object JavaDoc next() {
46         try {
47             final Object JavaDoc ret;
48             if (type == TYPE_OBJECT) {
49                 ret = persist.loadObject(objectClass, resultSet);
50             } else if (type == TYPE_MAP) {
51                 ret = Persist.loadMap(resultSet);
52             } else {
53                 ret = null;
54             }
55
56             hasNext = resultSet.next();
57             return ret;
58         } catch (SQLException JavaDoc e) {
59             throw new RuntimeSQLException(e);
60         }
61     }
62
63     public void remove() {
64         try {
65             this.resultSet.deleteRow();
66         } catch (SQLException JavaDoc e) {
67             throw new RuntimeSQLException(e);
68         }
69     }
70
71 }
72
Popular Tags