JDBC之关于数据库操作回滚研究(JDBC之四)

本文探讨了如何使用JDBC进行数据库操作回滚,包括检查数据库是否支持游标回滚,创建回滚数据结果集,查询结果集回滚类型,移动游标以及获取表的行数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查询数据库是否支持游标回滚操作
try {
        DatabaseMetaData dmd = connection.getMetaData();
        if (dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
            // Insensitive scrollable result sets are supported
        }
        if (dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
            // Sensitive scrollable result sets are supported
        }
        if (!dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)
            && !dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
            // Updatable result sets are not supported
        }
    } catch (SQLException e) {
    }
创建一个回滚得数据结果集
try {
        // Create an insensitive scrollable result set
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    
        // Create a sensitive scrollable result set
        stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    } catch (SQLException e) {
    }
查询一个数据结果集的回滚类型
try {
        // Get type of the result set
        int type = resultSet.getType();
    
        if (type == ResultSet.TYPE_SCROLL_INSENSITIVE
              || type == ResultSet.TYPE_SCROLL_SENSITIVE) {
            // Result set is scrollable
        } else {
            // Result set is not scrollable
        }
    } catch (SQLException e) {
    }
移动游标到指定的位置
try {
        // Create a scrollable result set
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    
        // Move cursor forward
        while (resultSet.next()) {
            // Get data at cursor
            String s = resultSet.getString(1);
        }
    
        // Move cursor backward
        while (resultSet.previous()) {
            // Get data at cursor
            String s = resultSet.getString(1);
        }
    
        // Move cursor to the first row
        resultSet.first();
    
        // Move cursor to the last row
        resultSet.last();
    
        // Move cursor to the end, after the last row
        resultSet.afterLast();
    
        // Move cursor to the beginning, before the first row.
        // cursor position is 0.
        resultSet.beforeFirst();
    
        // Move cursor to the second row
        resultSet.absolute(2);
    
        // Move cursor to the last row
        resultSet.absolute(-1);
    
        // Move cursor to the second last row
        resultSet.absolute(-2);
    
        // Move cursor down 5 rows from the current row.  If this moves
        // cursor beyond the last row, cursor is put after the last row
        resultSet.relative(5);
    
        // Move cursor up 3 rows from the current row.  If this moves
        // cursor beyond the first row, cursor is put before the first row
        resultSet.relative(-3);
    } catch (SQLException e) {
    }
获取游标的位置
try {
        // Create a scrollable result set
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    
        // Get cursor position
        int pos = resultSet.getRow();            // 0
        boolean b = resultSet.isBeforeFirst();   // true
    
        // Move cursor to the first row
        resultSet.next();
    
        // Get cursor position
        pos = resultSet.getRow();                // 1
        b = resultSet.isFirst();                 // true
    
        // Move cursor to the last row
        resultSet.last();
    
        // Get cursor position
        pos = resultSet.getRow();                // If table has 10 rows, value would be 10
        b = resultSet.isLast();                  // true
    
        // Move cursor past last row
        resultSet.afterLast();
    
        // Get cursor position
        pos = resultSet.getRow();                // If table has 10 rows, value would be 11
        b = resultSet.isAfterLast();             // true
    } catch (SQLException e) {
    }
获取数据库表中全部的行数
try {
        // Create a scrollable result set
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    
        // Move to the end of the result set
        resultSet.last();
    
        // Get the row number of the last row which is also the row count
        int rowCount = resultSet.getRow();
    } catch (SQLException e) {
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值