KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > jdbc > ResultSetImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28 package com.caucho.db.jdbc;
29
30 import com.caucho.db.sql.SelectResult;
31
32 import java.math.BigDecimal JavaDoc;
33 import java.sql.Blob JavaDoc;
34 import java.sql.Clob JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Time JavaDoc;
37 import java.sql.Timestamp JavaDoc;
38
39 /**
40  * The JDBC statement implementation.
41  */

42 public class ResultSetImpl extends AbstractResultSet {
43   private StatementImpl _stmt;
44   private SelectResult _rs;
45   private int _rowNumber;
46   
47   ResultSetImpl(StatementImpl stmt, SelectResult rs)
48   {
49     _stmt = stmt;
50     _rs = rs;
51   }
52
53   public int getRow()
54     throws SQLException JavaDoc
55   {
56     if (_rowNumber < 0)
57       throw new SQLException JavaDoc("can't call getRow() after close()");
58     
59     return _rowNumber;
60   }
61
62   public boolean isBeforeFirst()
63     throws SQLException JavaDoc
64   {
65     return _rowNumber == 0;
66   }
67
68   public boolean isFirst()
69     throws SQLException JavaDoc
70   {
71     return _rowNumber == 1;
72   }
73
74   public java.sql.Statement JavaDoc getStatement()
75     throws SQLException JavaDoc
76   {
77     if (_rowNumber < 0)
78       throw new SQLException JavaDoc("can't call getStatement() after close()");
79     
80     return _stmt;
81   }
82
83   public java.sql.ResultSetMetaData JavaDoc getMetaData()
84     throws SQLException JavaDoc
85   {
86     if (_rowNumber < 0)
87       throw new SQLException JavaDoc("can't call getMetaData() after close()");
88     
89     return new ResultSetMetaDataImpl(_rs);
90   }
91
92   public boolean wasNull()
93     throws SQLException JavaDoc
94   {
95     return _rs.wasNull();
96   }
97
98   /**
99    * Goes to the next row, returning true if it exists.
100    */

101   public boolean next()
102     throws SQLException JavaDoc
103   {
104     if (_rs == null)
105       return false;
106     else if (_rs.next())
107       return true;
108     else {
109       close();
110
111       return false;
112     }
113   }
114
115   public int findColumn(String JavaDoc columnName)
116     throws SQLException JavaDoc
117   {
118     return _rs.findColumnIndex(columnName);
119   }
120
121   /**
122    * Returns the boolean value for the column.
123    */

124   public boolean getBoolean(int columnIndex)
125     throws SQLException JavaDoc
126   {
127     String JavaDoc s = getString(columnIndex);
128     
129     return s != null && ! s.equals("") && ! s.equals("0") && ! s.equals("n");
130   }
131
132   /**
133    * Returns the date value for the column.
134    */

135   public java.sql.Date JavaDoc getDate(int columnIndex)
136     throws SQLException JavaDoc
137   {
138     long date = _rs.getDate(columnIndex - 1);
139
140     if (wasNull())
141       return null;
142     else
143       return new java.sql.Date JavaDoc(date);
144   }
145
146   /**
147    * Returns the double value for the column.
148    */

149   public double getDouble(int columnIndex)
150     throws SQLException JavaDoc
151   {
152     return _rs.getDouble(columnIndex - 1);
153   }
154
155   /**
156    * Returns the integer value for the column.
157    */

158   public int getInt(int columnIndex)
159     throws SQLException JavaDoc
160   {
161     return _rs.getInt(columnIndex - 1);
162   }
163
164   /**
165    * Returns the long value for the column.
166    */

167   public long getLong(int columnIndex)
168     throws SQLException JavaDoc
169   {
170     return _rs.getLong(columnIndex - 1);
171   }
172
173   /**
174    * Returns the string value for the column.
175    */

176   public String JavaDoc getString(int columnIndex)
177     throws SQLException JavaDoc
178   {
179     return _rs.getString(columnIndex - 1);
180   }
181
182   /**
183    * Returns the time value for the column.
184    */

185   public Time JavaDoc getTime(int columnIndex)
186     throws SQLException JavaDoc
187   {
188     long date = _rs.getDate(columnIndex - 1);
189
190     if (wasNull())
191       return null;
192     else
193       return new java.sql.Time JavaDoc(date);
194   }
195
196   public Timestamp JavaDoc getTimestamp(int columnIndex)
197     throws SQLException JavaDoc
198   {
199     long date = _rs.getDate(columnIndex - 1);
200
201     if (wasNull())
202       return null;
203     else
204       return new java.sql.Timestamp JavaDoc(date);
205   }
206
207   /**
208    * Returns the big-decimal value for the column.
209    */

210   public BigDecimal JavaDoc getBigDecimal(int columnIndex)
211     throws SQLException JavaDoc
212   {
213     return new BigDecimal JavaDoc(_rs.getString(columnIndex - 1));
214   }
215
216   /**
217    * Returns the blob value for the column.
218    */

219   public Blob JavaDoc getBlob(int columnIndex)
220     throws SQLException JavaDoc
221   {
222     return _rs.getBlob(columnIndex - 1);
223   }
224
225   /**
226    * Returns the clob value for the column.
227    */

228   public Clob JavaDoc getClob(int columnIndex)
229     throws SQLException JavaDoc
230   {
231     return _rs.getClob(columnIndex - 1);
232   }
233
234   public void close()
235     throws SQLException JavaDoc
236   {
237     SelectResult result = _rs;
238     _rs = null;
239
240     if (result != null)
241       result.close();
242   }
243 }
244
Popular Tags