KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > model > sql > ResultSetIterator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.table.model.sql;
16
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 /**
25  *
26  * @author mindbridge
27  */

28 public class ResultSetIterator implements Iterator JavaDoc
29 {
30     private static final Log LOG = LogFactory.getLog(ResultSetIterator.class);
31
32     private ResultSet JavaDoc m_objResultSet;
33     private boolean m_bFetched;
34     private boolean m_bAvailable;
35
36     public ResultSetIterator(ResultSet JavaDoc objResultSet)
37     {
38         m_objResultSet = objResultSet;
39         m_bFetched = false;
40     }
41
42     /**
43      * @see java.util.Iterator#hasNext()
44      */

45     public synchronized boolean hasNext()
46     {
47         if (getResultSet() == null) return false;
48         
49         if (!m_bFetched)
50         {
51             m_bFetched = true;
52
53             try
54             {
55                 m_bAvailable = !getResultSet().isLast();
56             }
57             catch (SQLException JavaDoc e)
58             {
59                 LOG.warn(
60                     "SQLException while testing for end of the ResultSet",
61                     e);
62                 m_bAvailable = false;
63             }
64
65             if (!m_bAvailable)
66                 notifyEnd();
67         }
68
69         return m_bAvailable;
70     }
71
72     /**
73      * @see java.util.Iterator#next()
74      */

75     public synchronized Object JavaDoc next()
76     {
77         ResultSet JavaDoc objResultSet = getResultSet();
78
79         try
80         {
81             if (!objResultSet.next())
82                 return null;
83         }
84         catch (SQLException JavaDoc e)
85         {
86             LOG.warn("SQLException while iterating over the ResultSet", e);
87             return null;
88         }
89
90         m_bFetched = false;
91         return objResultSet;
92     }
93
94     /**
95      * @see java.util.Iterator#remove()
96      */

97     public void remove()
98     {
99         try
100         {
101             getResultSet().deleteRow();
102         }
103         catch (SQLException JavaDoc e)
104         {
105             LOG.error("Cannot delete record", e);
106         }
107     }
108
109     /**
110      * Returns the resultSet.
111      * @return ResultSet
112      */

113     public ResultSet JavaDoc getResultSet()
114     {
115         return m_objResultSet;
116     }
117
118     protected void notifyEnd()
119     {
120     }
121
122 }
123
Popular Tags