Java代碼工具箱之超出遊標最大數

來源:互聯網
上載者:User

標籤:date()   read   工具箱   span   oid   color   領會   resultset   update   

1. Java大量寫入oracle時容易出現此錯。經過此錯,也觸動自己要深刻理解 java 的 prepareStatement 等對象,及資料庫的串連與釋放。

2. 原因:經常會出現在 for 迴圈中進行資料庫寫入操作。

3. 代碼:

案例:我在將Excel匯入到Oracle資料庫中時,由於搜尋網上代碼,未深刻領會。在遍曆Excel的每一行資料時,都執行

 pre = con.prepareStatement(sql) 。直到Excel中的5000行資料遍曆完後才 執行   pre.close(); ,結果報錯,出現

超出遊標錯誤!後來更正為在 for 迴圈內部,執行完  pre.executeQuery(); 緊接著,立即執行  pre.close(); (當然關閉前還要有

個 pre 判空的判斷語句)。更正後,一次順利匯入5000條資料。

class myCode{Connection con = null;PreparedStatement pre = null;ResultSet result = null;void readExcel(){for(...)     for(...)           update();}void update(){    String sql = "update myTabe set A=? where id=?";    pre = conn.prepareStatement(sql);    pre.setString(1,A);    pre.setLong(2, 123);        pre.executeUpdate();    //報錯}static void main(String[] args){      connDB()      initDbObj()      readExcel()}}

正確的 update()函數應該如下:

//正確代碼void update(){    String sql = "update myTabe set A=? where id=?";    pre = conn.prepareStatement(sql);    pre.setString(1,A);    pre.setLong(2, 123);        pre.executeUpdate();    //重點    if(  pre != null ){        pre.close();    }}//pre對象用完必須立馬關閉,不要放到最後和 conn一起關閉。//否則,每次pre被設定一次,就會申請一個遊標,一般遊標才300個//很快就會報錯。

 

Java代碼工具箱之超出遊標最大數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.