1 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 ; 29 import java.sql.SQLException ; 30 31 36 public class ScrollableResultSetTable extends AbstractResultSetTable 37 { 38 private final int _rowCount; 39 40 public ScrollableResultSetTable(ITableMetaData metaData, ResultSet resultSet) 41 throws SQLException , DataSetException 42 { 43 super(metaData, resultSet); 44 45 try 46 { 47 if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) 48 { 49 throw new SQLException ("Forward only ResultSet not supported"); 50 } 51 52 _resultSet.last(); 53 _rowCount = _resultSet.getRow(); 54 } 55 catch (SQLException e) 56 { 57 close(); 58 throw e; 59 } 60 } 61 62 public ScrollableResultSetTable(ITableMetaData metaData, 63 IDatabaseConnection connection) throws DataSetException, SQLException 64 { 65 super(metaData, connection); 66 67 try 68 { 69 if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) 70 { 71 throw new SQLException ("Forward only ResultSet not supported"); 72 } 73 74 _resultSet.last(); 75 _rowCount = _resultSet.getRow(); 76 } 77 catch (SQLException e) 78 { 79 close(); 80 throw e; 81 } 82 } 83 84 public ScrollableResultSetTable(String tableName, String selectStatement, 85 IDatabaseConnection connection) throws DataSetException, SQLException 86 { 87 super(tableName, selectStatement, connection); 88 89 try 90 { 91 if (_resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) 92 { 93 throw new SQLException ("Forward only ResultSet not supported"); 94 } 95 96 _resultSet.last(); 97 _rowCount = _resultSet.getRow(); 98 } 99 catch (SQLException e) 100 { 101 close(); 102 throw e; 103 } 104 } 105 106 109 public int getRowCount() 110 { 111 return _rowCount; 112 } 113 114 public Object getValue(int row, String 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 e) 127 { 128 throw new DataSetException(e); 129 } 130 } 131 } 132 133 134 135 136 137 138 | Popular Tags |