<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    推薦淘寶秋冬男裝熱賣網店

    追求無止境

    我的程序人生
    隨筆 - 31, 文章 - 2, 評論 - 20, 引用 - 0
    數據加載中……

    JDBC高級特性

    1、可滾動的結果集(Scrollable Result Sets)

    (1)創建可滾動的結果集:

      Statement stmt = con.createStatement(
                           ResultSet.TYPE_SCROLL_INSENSITIVE, 
                           ResultSet.CONCUR_READ_ONLY);
    ResultSet.TYPE_FORWARD_ONLY:結果集是不能滾動的;他的游標只能向前移動;
    ResultSet.TYPE_SCROLL_INSENSITIVE:結果是可滾動的;游標可以向前也可以后退。也可以移動到一個絕對位置。
    ResultSet.TYPE_SCROLL_SENSITIVE:結果是可滾動的;游標可以向前也可以后退。也可以移動到一個絕對位置。
    ResultSet.CONCUR_READ_ONLY:結果集是只讀的。
    ResultSet.ResultSet.CONCUR_UPDATABLE:結果集是可以更新的。
    
      ResultSet srs = stmt.executeQuery("SELECT COF_NAME, 
                           PRICE FROM COFFEES");
    盡管我們可以在這里設置創建的是可滾動結果集,但是如果廠商的JDBC實現不支持,我們獲取到的結果將不具有可滾動屬性。
    可以使用ResultSet.getType()方法來獲取是否支持滾動:
     int type = rs.getType();
    

    The variable type will be one of the following:

    1003 to indicate ResultSet.TYPE_FORWARD_ONLY

    1004 to indicate ResultSet.TYPE_SCROLL_INSENSITIVE

    1005 to indicate ResultSet.TYPE_SCROLL_SENSITIVE

    TYPE_SCROLL_INSENSITIVE和TYPE_SCROLL_SENSITIVE的主要區別是在如果發生改變他們的敏感度。前一個將不會很敏感后一個則會。

    (2)移動游標,使用以下方法可以移動游標:

    rs.next();

    rs.previous();

    rs.absolute();

    rs.relative();

    rs.first();

    rs.last();

    rs.beforeFirst();

    rs.afterLast();

    使用rs.getRow()獲取游標當前行。

    rs.isAfterLast();
    rs.isBeforeFirst();
    rs.isLast();
    rs.isFirst();
    rs.hasNext();
    等等方法。
    2、更新結果集
    (1)創建可以更新的結果集
    Statement stmt = con.createStatement(
                   ResultSet.TYPE_SCROLL_SENSITIVE, 
                   ResultSet.CONCUR_UPDATABLE);
      ResultSet uprs = stmt.executeQuery(
                   "SELECT COF_NAME, 
                   PRICE FROM COFFEES");

    在JDBC 2.0中,我們可以向可以更新的結果集中插入行或刪除行,或者修改其中的行。

    下面的方法用于判斷結果集是否可以更新:

      int concurrency = uprs.getConcurrency();
    

    The variable concurrency will be one of the following:

    1007 to indicate ResultSet.CONCUR_READ_ONLY

    1008 to indicate ResultSet.CONCUR_UPDATABLE

    (2)更新結果集

    JDBC 1.0中可以這樣更新: 
     stmt.executeUpdate(
        "UPDATE COFFEES SET PRICE = 10.99 " +
        "WHERE COF_NAME = 'French_Roast_Decaf'");
    在JDBC2.0中。則可以:
     uprs.last();
      uprs.updateFloat("PRICE", 10.99f);
    uprs.updateRow();
    在移動游標前,必須先調用updateRow方法。否則更新信息會丟失。調用cancelRowUpdates可以取消對行的更新。
    (3)向結果集中插入或者刪除行
      Connection con = DriverManager.getConnection(
                          "jdbc:mySubprotocol:mySubName");
      Statement stmt = con.createStatement(
                           ResultSet.TYPE_SCROLL_SENSITIVE, 
                           ResultSet.CONCUR_UPDATABLE);
      ResultSet uprs = stmt.executeQuery(
                          "SELECT * FROM COFFEES");
      uprs.moveToInsertRow();
    
      uprs.updateString("COF_NAME", "Kona");
      uprs.updateInt("SUP_ID", 150);
      uprs.updateFloat("PRICE", 10.99f);
      uprs.updateInt("SALES", 0);
      uprs.updateInt("TOTAL", 0);
      
      uprs.insertRow();
    在移動游標前,必須要先調用insertRow否則插入的信息將丟失。
    uprs.absolute(4);
    uprs.deleteRow();
    刪除行。
    (4)查看結果集中的變化(其實就是說了一個意思,用TYPE_SCROLL_SENSITIVE對數據很敏感,一旦數據變化就會反映在ResultSet中)

    Result sets vary greatly in their ability to reflect changes made in their underlying data. If you modify data in a ResultSet object, the change will always be visible if you close it and then reopen it during a transaction. In other words, if you re-execute the same query after changes have been made, you will produce a new result set based on the new data in the target table. This new result set will naturally reflect changes you made earlier. You will also see changes made by others when you reopen a result set if your transaction isolation level makes them visible.

    So when can you see visible changes you or others made while the ResultSet object is still open? (Generally, you will be most interested in the changes made by others because you know what changes you made yourself.) The answer depends on the type of ResultSet object you have.

    With a ResultSet object that is TYPE_SCROLL_SENSITIVE, you can always see visible updates made to existing column values. You may see inserted and deleted rows, but the only way to be sure is to use DatabaseMetaData methods that return this information. ("New JDBC 2.0 Core API Features" on page 371 explains how to ascertain the visibility of changes.)

    You can, to some extent, regulate what changes are visible by raising or lowering the transaction isolation level for your connection with the database. For example, the following line of code, wherecon is an active Connection object, sets the connection's isolation level to TRANSACTION_READ_COMMITTED:

      con.setTransactionIsolation(
                  Connection.TRANSACTION_READ_COMMITTED);
    

    With this isolation level, a TYPE_SCROLL_SENSITIVE result set will not show any changes before they are committed, but it can show changes that may have other consistency problems. To allow fewer data inconsistencies, you could raise the transaction isolation level to TRANSACTION_REPEATABLE_READ. The problem is that, in most cases, the higher the isolation level, the poorer the performance is likely to be. And, as is always true of JDBC drivers, you are limited to the levels your driver actually provides. Many programmers find that the best choice is generally to use their database's default transaction isolation level. You can get the default with the following line of code, where con is a newly-created connection:

      int level = con.getTransactionIsolation();
    

    The explanation of Connection fields, beginning on page 347, gives the transaction isolation levels and their meanings.

    If you want more information about the visibility of changes and transaction isolation levels, see "What Is Visible to Transactions" on page 597.

    In a ResultSet object that is TYPE_SCROLL_INSENSITIVE, you cannot see changes made to it by others while it is still open, but you may be able to see your own changes with some implementations. This is the type of ResultSet object to use if you want a consistent view of data and do not want to see changes made by others.

    posted on 2009-11-16 13:05 追求無止境 閱讀(279) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 伊人久久亚洲综合影院首页| 狠狠色伊人亚洲综合网站色| 日韩在线一区二区三区免费视频| 全免费a级毛片免费看无码| 亚洲成人动漫在线观看| 国内精品免费麻豆网站91麻豆 | 亚洲另类视频在线观看| 中文字幕乱码免费视频| 国产.亚洲.欧洲在线| 最近中文字幕mv免费高清视频7| 亚洲欧美日韩中文字幕在线一区| 性色av免费观看| 黄页网站在线视频免费| 亚洲日本va午夜中文字幕久久| 精精国产www视频在线观看免费| 亚洲人成亚洲人成在线观看| 国产激情免费视频在线观看| 亚洲精品成人网站在线播放| 无码免费午夜福利片在线| 亚洲精品无码久久久久牙蜜区| 国产一区二区三区在线观看免费| 一本大道一卡二大卡三卡免费| 国产亚洲av片在线观看18女人| 大地影院MV在线观看视频免费 | 亚洲无码视频在线| 国产又黄又爽胸又大免费视频| 亚洲国产精品久久久久久| 野花高清在线观看免费完整版中文 | 免费人成激情视频| 美女巨胸喷奶水视频www免费| 久久久久久亚洲精品成人| 1024免费福利永久观看网站| 蜜桃传媒一区二区亚洲AV| 亚洲人成网77777色在线播放| 无码乱肉视频免费大全合集| 在线观看亚洲专区| 亚洲日韩区在线电影| 四色在线精品免费观看| 中文字幕无码一区二区免费| 一本天堂ⅴ无码亚洲道久久| 亚洲中文久久精品无码|