`

Java JDBC基础(三)

阅读更多

 

package com.yli.demo;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Statement测试[ResultSet可滚动可更新结果集]<br>
 * 可滚动:对ResultSet可向前向后定位<br>
 * 可更新:ResultSet数据修改后可提交至数据库
 */
public class ScrollStatementTest {
    
    public static void main(String[] args) {
        Test1();
    }
    
    /**
     * 测试可滚动,可更新的结果集ResultSet
     */
    public static void Test1() {
        try {
            Connection conn;
            conn = ConnectionUtil.getConnection();
            
            /**
             * 
             * ResultSet.TYPE_FORWARD_ONLY [结果集不可滚动]
             * ResultSet.TYPE_SCROLL_INSENSITIVE [结果集可滚动,且不可感知查询出来的数据是否有变化]
             * ResultSet.TYPE_SCROLL_SENSITIVE [结果集可滚动,可感知查询出来的数据是否有变化]
             * 
             * ResultSet.CONCUR_READ_ONLY[结果集只读]
             * ResultSet.CONCUR_UPDATABLE[结果集可更新至数据库]
             */
            
            // 创建[可滚动对数据更新不敏感]+[结果集可更新至数据库]的Statement
            Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            
            String sql = "select afficheid,affichetitle from ES_T_SHOP_AFFICHE";
            ResultSet rs = statement.executeQuery(sql);
            
            // 检查是否可支持是否支持滚动与更新
            DatabaseMetaData meta = conn.getMetaData();
            boolean isSupport = meta.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
            System.out.println("===========>isSupport=" + isSupport);
            
            long id;
            String title;
            int currentRow;
            boolean isAvailable;
            
            /**
             * 像使用数组一样使用结果集ResultSet
             * absolute(rowId) 定位到第几行
             * previous()   前一行数据
             * next()   后一行数据
             * getRow() 返回行号,如果返回0表示不可用:即在第一行之前或者最后一行之后
             * 
             * 如果当前一行已经在第一行,再回到前一行则返回false
             * 如果当前一行已经在最后一行,再回到后一行则返回false
             */
            
            // 定位到第1行数据[从1开始计算]
            isAvailable = rs.absolute(1);   // 同时返回值表示该行是否可用
            id = rs.getLong("afficheid");
            title = rs.getString("affichetitle");
            currentRow = rs.getRow();
            
            System.out.println("============>更新之前");
            System.out.println("============>是否可用:" + isAvailable);
            System.out.println("============>当前行号:" + currentRow);
            System.out.println("============>id:" + id);
            System.out.println("============>title:" + title);
            System.out.println();
            
            // 更改第一行数据[可更新]
            rs.updateString("affichetitle", "IT-EYE");  // ResultSet数据集已更新
            rs.updateLong("afficheid", id);  // ResultSet数据集已更新
            rs.updateRow();     // ResultSet更新提交至数据库
            
            id = rs.getLong("afficheid");
            title = rs.getString("affichetitle");
            currentRow = rs.getRow();
            
            System.out.println("============>更新之后");
            System.out.println("============>是否可用:" + isAvailable);
            System.out.println("============>当前行号:" + currentRow);
            System.out.println("============>id:" + id);
            System.out.println("============>title:" + title);
            System.out.println();
            
            
            // 定位到第5行数据
            isAvailable = rs.absolute(5);   // 同时返回值表示该行是否可用
            id = rs.getLong("afficheid");
            title = rs.getString("affichetitle");
            currentRow = rs.getRow();
            
            System.out.println("============>是否可用:" + isAvailable);
            System.out.println("============>当前行号:" + currentRow);
            System.out.println("============>id:" + id);
            System.out.println("============>title:" + title);
            System.out.println();
            
            // 定位到前一行
            isAvailable = rs.previous();   // 同时返回值表示该行是否可用
            
            id = rs.getLong("afficheid");
            title = rs.getString("affichetitle");
            currentRow = rs.getRow();
            
            System.out.println("============>是否可用:" + isAvailable);
            System.out.println("============>当前行号:" + currentRow);
            System.out.println("============>id:" + id);
            System.out.println("============>title:" + title);
            System.out.println();
            
            // 定位到后一行
            isAvailable = rs.next();
            
            id = rs.getLong("afficheid");
            title = rs.getString("affichetitle");
            currentRow = rs.getRow();
            
            System.out.println("============>是否可用:" + isAvailable);
            System.out.println("============>当前行号:" + currentRow);
            System.out.println("============>id:" + id);
            System.out.println("============>title:" + title);
            System.out.println();
            
            /**
             * 快捷方法
             * first() 回到第一行
             * isFirst() 是否在第一行
             * 
             * beforeFist() 回到第一行之前,那么getRow()其实为0,因为这一行前面肯定没数据
             * ifBeforeFirst() 是否在第一行之前
             * 
             * afterLast() 回到最后一行之后,那么getRow()其实为0,因为这一行后面肯定没数据
             * isAfterLast() 是否在第一行之前
             */
            
            boolean isFirst;
            boolean isLast;
            boolean ifBeforeFirst;
            boolean isAfterLast;
            
            // 定位到第一行
            isAvailable = rs.first();
            isFirst = rs.isFirst();
            isLast = rs.isLast();
            ifBeforeFirst = rs.isBeforeFirst();
            isAfterLast = rs.isAfterLast();
            
            id = rs.getLong("afficheid");
            title = rs.getString("affichetitle");
            currentRow = rs.getRow();
            
            System.out.println("============>是否可用:" + isAvailable);
            System.out.println("============>当前行号:" + currentRow);
            System.out.println("============>是否第一行:" + isFirst);
            System.out.println("============>是否第一行之前:" + ifBeforeFirst);
            System.out.println("============>是否最后一行:" + isLast);
            System.out.println("============>是否最后一行之后:" + isAfterLast);
            System.out.println("============>id:" + id);
            System.out.println("============>title:" + title);
            System.out.println();
            
            
            // 使用完毕之后ResultSet还是完整的ResultSet,跟数组一样
            // 仍然可以普通方法使用ResultSet
            while(rs.next()){
                // 正常从第一行读取,也可以使用updateRow()更新结果集
                // do...
            }
            
            ConnectionUtil.close(conn);
           
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics