JDBC 更新数据结构集操作(JDBC之四)

查询结果集是否支持更新操作
try {
        DatabaseMetaData dmd = connection.getMetaData();
        if (dmd.supportsResultSetConcurrency(
            ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
            // Updatable result sets are supported
   
   
        } else {
            // Updatable result sets are not supported
   
   
        }
    } catch (SQLException e) {
    }
创建一个可以更新的结果集
try {
        // Create a statement that will return updatable result sets
   
   
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    
        // Primary key col_string must be specified so that the result set is updatable
   
   
        ResultSet resultSet = stmt.executeQuery("SELECT col_string FROM my_table");
    } catch (SQLException e) {
    }
查询结果是否更新
try {
        // Get concurrency of the result set
   
   
        int concurrency = resultSet.getConcurrency();
    
        if (concurrency == ResultSet.CONCUR_UPDATABLE) {
            // Result set is updatable
   
   
        } else {
            // Result set is not updatable
   
   
        }
    } catch (SQLException e) {
    }
利用结果集更新行数据
try {
        // Create an updatable result set
   
   
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    
        // Move cursor to the row to update
   
   
        resultSet.first();
    
        // Update the value of column col_string on that row
   
   
        resultSet.updateString("col_string", "new data");
    
        // Update the row; if auto-commit is enabled, update is committed
   
   
        resultSet.updateRow();
    } catch (SQLException e) {
    }
取消结果集的更新操作
try {
        // Create an updatable result set
   
   
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    
        // Move cursor to the row to update
   
   
        resultSet.first();
    
        // Update the value of column col_string on that row
   
   
        resultSet.updateString("col_string", "new data");
    
        // Discard the update to the row
   
   
        resultSet.cancelRowUpdates();
    } catch (SQLException e) {
    }
利用结果集删除数据行
try {
        // Create an updatable result set
   
   
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    
        // Delete the first row
   
   
        resultSet.first();
        resultSet.deleteRow();
    } catch (SQLException e) {
    }
刷新可更新的结果集
try {
        // Create an updatable result set
   
   
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    
        // Use the result set...
   
   
    
        // Retrieve the current values of the row from the database
   
   
        resultSet.refreshRow();
    } catch(SQLException e) {
    }
插入数据利用可更新的数据结果集
try {
        // Create an updatable result set
   
   
        Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    
        // Move cursor to the "insert row"
   
   
        resultSet.moveToInsertRow();
    
        // Set values for the new row.
   
   
        resultSet.updateString("col_string", "new data");
    
        // Insert the row
   
   
        resultSet.insertRow();
    } catch (SQLException e) {
    }
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值