KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > ScrollableResultSetTable


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.database;
23
24 import org.dbunit.dataset.Column;
25 import org.dbunit.dataset.DataSetException;
26 import org.dbunit.dataset.ITableMetaData;
27
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 /**
32  * @author Manuel Laflamme
33  * @version $Revision: 1.6 $
34  * @since Feb 17, 2002
35  */

36 public class ScrollableResultSetTable extends AbstractResultSetTable
37 {
38     private final int _rowCount;
39
40     public ScrollableResultSetTable(ITableMetaData metaData, ResultSet JavaDoc resultSet)
41             throws SQLException JavaDoc, DataSetException
42     {
43         super(metaData, resultSet);
44
45         try
46         {
47             if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
48             {
49                 throw new SQLException JavaDoc("Forward only ResultSet not supported");
50             }
51
52             _resultSet.last();
53             _rowCount = _resultSet.getRow();
54         }
55         catch (SQLException JavaDoc e)
56         {
57             close();
58             throw e;
59         }
60     }
61
62     public ScrollableResultSetTable(ITableMetaData metaData,
63             IDatabaseConnection connection) throws DataSetException, SQLException JavaDoc
64     {
65         super(metaData, connection);
66
67         try
68         {
69             if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
70             {
71                 throw new SQLException JavaDoc("Forward only ResultSet not supported");
72             }
73
74             _resultSet.last();
75             _rowCount = _resultSet.getRow();
76         }
77         catch (SQLException JavaDoc e)
78         {
79             close();
80             throw e;
81         }
82     }
83
84     public ScrollableResultSetTable(String JavaDoc tableName, String JavaDoc selectStatement,
85             IDatabaseConnection connection) throws DataSetException, SQLException JavaDoc
86     {
87         super(tableName, selectStatement, connection);
88
89         try
90         {
91             if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
92             {
93                 throw new SQLException JavaDoc("Forward only ResultSet not supported");
94             }
95
96             _resultSet.last();
97             _rowCount = _resultSet.getRow();
98         }
99         catch (SQLException JavaDoc e)
100         {
101             close();
102             throw e;
103         }
104     }
105
106     ////////////////////////////////////////////////////////////////////////////
107
// ITable interface
108

109     public int getRowCount()
110     {
111         return _rowCount;
112     }
113
114     public Object JavaDoc getValue(int row, String JavaDoc columnName) throws DataSetException
115     {
116         assertValidRowIndex(row);
117
118         try
119         {
120             _resultSet.absolute(row + 1);
121
122             int columnIndex = getColumnIndex(columnName);
123             Column column = _metaData.getColumns()[columnIndex];
124             return column.getDataType().getSqlValue(columnIndex + 1, _resultSet);
125         }
126         catch (SQLException JavaDoc e)
127         {
128             throw new DataSetException(e);
129         }
130     }
131 }
132
133
134
135
136
137
138
Popular Tags