下面列出了java.sql.ResultSet#getType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Assert that a ResultSet representing generated keys is non-null
* and of the correct type. This method leaves the ResultSet
* open and does not fetch any date from it.
*
* @param description For assert messages
* @param keys ResultSet returned from getGeneratedKeys().
* @throws SQLException
*/
public static void assertGeneratedKeyResultSet(
String description, ResultSet keys) throws SQLException {
Assert.assertNotNull(description, keys);
// Requirements from section 13.6 JDBC 4 specification
assertEquals(
description +
" - Required CONCUR_READ_ONLY for generated key result sets",
ResultSet.CONCUR_READ_ONLY, keys.getConcurrency());
int type = keys.getType();
if ((type != ResultSet.TYPE_FORWARD_ONLY) &&
(type != ResultSet.TYPE_SCROLL_INSENSITIVE)) {
Assert.fail(description +
" - Invalid type for generated key result set" + type);
}
}
/**
* 解析resultset中的条数
*
* @param resultSet
* @return
*/
private String sqlResultSetParse(ResultSet rset) {
try {
// 只能向前遍历的游标,只能用next()来遍历
if (rset.getType() == ResultSet.TYPE_FORWARD_ONLY) {
return "FORWARD_ONLY";
}
else {
// 可以滚动的游标,在用户使用游标之前,直接获取结果集大小,然后游标回到开始的地方(假装游标没有移动过)
rset.last();
int row = rset.getRow();
rset.beforeFirst();
return row + "";
}
}
catch (SQLException e) {
return e.toString();
}
}
private int getResultNumber(ResultSet rs, long maxRecToParse, int recCount) throws SQLException {
logger.debug("IN");
int toReturn;
logger.debug("resultset type [" + rs.getType() + "] (" + (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) + ")");
if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
int recordsCount = 0;
if (recCount < maxRecToParse) {
// records read where less then max records to read, therefore the resultset has been completely read
recordsCount = getOffset() + recCount;
} else {
recordsCount = rs.getRow();
while (rs.next()) {
recordsCount++;
// do nothing, just scroll result set
}
}
toReturn = recordsCount;
} else {
rs.last();
toReturn = rs.getRow();
}
logger.debug("Reading total record numeber is equal to [" + toReturn + "]");
logger.debug("OUT " + toReturn);
return toReturn;
}
private int getResultNumber(ResultSet rs, long maxRecToParse, int recCount) throws SQLException {
logger.debug("IN");
int toReturn = recCount;
int remaining = 0;
logger.debug("resultset type [" + rs.getType() + "] (" + (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) + ")");
if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
int recordsReaded = 0;
if (recCount < maxRecToParse) {
// records read where less then max records to read, therefore the resultset has been completely read
recordsReaded = recCount;
} else {
recordsReaded = getFetchSize();
// recordsCount = rs.getRow();
while (rs.next()) {
remaining++;
// do nothing, just scroll result set
}
}
toReturn = getOffset() + recordsReaded + remaining;
}
logger.debug("Reading total record numeber is equal to [" + toReturn + "]");
logger.debug("OUT " + toReturn);
return toReturn;
}
public void init(ResultSet result) throws SQLException, PersistenceException {
int rowNumber = (pageNumber * pageSize) + 1;
if(result.getType() == java.sql.ResultSet.TYPE_FORWARD_ONLY){
for(int i=0; i<rowNumber; i++){
result.next();
}
} else {
result.absolute(rowNumber);
}
}
public DatabaseMetaDataResultSet(final ResultSet resultSet, final Collection<ShardingSphereRule> rules) throws SQLException {
this.resultSet = resultSet;
this.rules = rules;
type = resultSet.getType();
concurrency = resultSet.getConcurrency();
resultSetMetaData = resultSet.getMetaData();
columnLabelIndexMap = initIndexMap();
databaseMetaDataObjectIterator = initIterator(resultSet);
}
private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET) {
rs.absolute(rowBounds.getOffset());
}
} else {
for (int i = 0; i < rowBounds.getOffset(); i++) {
rs.next();
}
}
}
private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET) {
rs.absolute(rowBounds.getOffset());
}
} else {
for (int i = 0; i < rowBounds.getOffset(); i++) {
rs.next();
}
}
}
/**
* Creates a table model by using the given <code>ResultSet</code> as the backend. If the <code>ResultSet</code> is
* scrollable (the type is not <code>TYPE_FORWARD_ONLY</code>), an instance of {@link
* org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.sql.ScrollableResultSetTableModel} is returned.
* This model uses the extended capabilities of scrollable result sets to directly read data from the database without
* caching or the need of copying the complete <code>ResultSet</code> into the programs memory.
* <p/>
* If the <code>ResultSet</code> lacks the scrollable features, the data will be copied into a
* <code>DefaultTableModel</code> and the <code>ResultSet</code> gets closed.
*
* @param rs the result set.
* @param columnNameMapping defines, whether to use column names or column labels to compute the column index. If
* true, then we map the Name. If false, then we map the Label
* @param closeStatement a flag indicating whether closing the resultset should also close the statement.
* @return a closeable table model.
* @throws SQLException if there is a problem with the result set.
*/
public CloseableTableModel createTableModel( final ResultSet rs,
final boolean columnNameMapping,
final boolean closeStatement )
throws SQLException {
// Allow for override, some jdbc drivers are buggy :(
final String prop =
ClassicEngineBoot.getInstance().getGlobalConfig().getConfigProperty(
ResultSetTableModelFactory.RESULTSET_FACTORY_MODE, "auto" ); //$NON-NLS-1$
if ( "simple".equalsIgnoreCase( prop ) ) { //$NON-NLS-1$
return generateDefaultTableModel( rs, columnNameMapping );
}
int resultSetType = ResultSet.TYPE_FORWARD_ONLY;
try {
resultSetType = rs.getType();
} catch ( SQLException sqle ) {
ResultSetTableModelFactory.logger.info(
"ResultSet type could not be determined, assuming default table model." ); //$NON-NLS-1$
}
if ( resultSetType == ResultSet.TYPE_FORWARD_ONLY ) {
return generateDefaultTableModel( rs, columnNameMapping );
} else {
rs.last();
int rowCount = rs.getRow();
rs.beforeFirst();
if ( rowCount < 500 ) {
return generateDefaultTableModel( rs, columnNameMapping );
}
return new ScrollableResultSetTableModel( rs, columnNameMapping, closeStatement );
}
}
private void loadDataFrom(final DataViewPageContext pageContext, ResultSet rs) throws SQLException, InterruptedException {
if (rs == null) {
return;
}
int pageSize = pageContext.getPageSize();
int startFrom;
if (useScrollableCursors ) {
startFrom = pageContext.getCurrentPos(); // will use rs.absolute
} else if (!limitSupported || isLimitUsedInSelect(dataView.getSQLString())) {
startFrom = pageContext.getCurrentPos(); // need to use slow skip
} else {
startFrom = 0; // limit added to select, can start from first item
}
final List<Object[]> rows = new ArrayList<>();
int colCnt = pageContext.getTableMetaData().getColumnCount();
int curRowPos = 0;
try {
long start = System.currentTimeMillis();
boolean hasNext = false;
boolean needSlowSkip = true;
if (useScrollableCursors
&& (rs.getType() == ResultSet.TYPE_SCROLL_INSENSITIVE
|| rs.getType() == ResultSet.TYPE_SCROLL_SENSITIVE)) {
try {
hasNext = rs.absolute(startFrom);
curRowPos = rs.getRow();
needSlowSkip = false;
} catch (SQLException ex) {
LOGGER.log(Level.FINE, "Absolute positioning failed", ex); // NOI18N
}
}
if (needSlowSkip) {
// Skip till current position
hasNext = rs.next();
curRowPos++;
while (hasNext && curRowPos < startFrom) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
hasNext = rs.next();
curRowPos++;
}
}
// Get next page
int rowCnt = 0;
while (((pageSize <= 0) || (pageSize > rowCnt)) && (hasNext)) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
Object[] row = new Object[colCnt];
for (int i = 0; i < colCnt; i++) {
row[i] = DBReadWriteHelper.readResultSet(rs,
pageContext.getTableMetaData().getColumn(i), i + 1);
}
rows.add(row);
rowCnt++;
try {
hasNext = rs.next();
curRowPos++;
} catch (SQLException x) {
LOGGER.log(Level.INFO, "Failed to forward to next record, cause: " + x.getLocalizedMessage(), x);
hasNext = false;
}
}
long end = System.currentTimeMillis();
dataView.addFetchTime(end - start);
} catch (SQLException e) {
LOGGER.log(Level.INFO, "Failed to set up table model.", e); // NOI18N
throw e;
} finally {
Mutex.EVENT.writeAccess(new Runnable() {
@Override
public void run() {
pageContext.getModel().setData(rows);
pageContext.getModel().setRowOffset(pageContext.getCurrentPos() - 1);
}
});
}
}
/**
* Check that the cursor is scrollable.
*
* @param rs the ResultSet to check
* @param operation which operation this is checked for
* @exception ijException if the cursor isn't scrollable
* @exception SQLException if a database error occurs
*/
private void checkScrollableCursor(ResultSet rs, String operation)
throws ijException, SQLException {
if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
throw ijException.forwardOnlyCursor(operation);
}
}
/**
* Check that the cursor is scrollable.
*
* @param rs the ResultSet to check
* @param operation which operation this is checked for
* @exception ijException if the cursor isn't scrollable
* @exception SQLException if a database error occurs
*/
private void checkScrollableCursor(ResultSet rs, String operation)
throws ijException, SQLException {
if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
throw ijException.forwardOnlyCursor(operation);
}
}
/**
* Check that the cursor is scrollable.
*
* @param rs the ResultSet to check
* @param operation which operation this is checked for
* @exception ijException if the cursor isn't scrollable
* @exception SQLException if a database error occurs
*/
private void checkScrollableCursor(ResultSet rs, String operation)
throws ijException, SQLException {
if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
throw ijException.forwardOnlyCursor(operation);
}
}