2013-10-04 34 views
1

我已經連接到Oracle數據庫。 現在我面臨Oracle Open遊標或ora-1000錯誤,「超出最大打開遊標數」。

ORA-01000: maximum open cursors exceeded 

我使用的代碼中插入數據:

public static void OracleJDBC(String Serial_Number, String Charged_MDN) { 

    String dataInsertQuery = "insert into CDR_Huawei (Serial_Number, Charged_MDN) values ('" + Serial_Number + "', '" + Charged_MDN + "')"; 
    String dataSelectQuery = "select * from CDR_Huawei"; 
    Statement statement = null; 

    try { 
    statement = connection.createStatement(); 
    statement.execute(dataInsertQuery); 
    //System.out.println("Data Inserted Successfully"); 

    } catch (SQLException e) { 
      e.printStackTrace(); 
    } 
    } 

它僅適用於前500條記錄,然後我有錯誤ORA-1000。 我總共有約6000條記錄。 我發現一些話題說應該改變配置,但是我不能改變配置。

有沒有解決這個錯誤的另一種方法?

+1

寫將statement.close()......執行後...它來了,因爲你沒有關閉它 –

+3

請。這不是PHP,並且以這種方式創建查詢即使在那裏也是皺眉頭......使用PreparedStatements。您稍後會感謝您的決定,相信我... – ppeterka

回答

3

finally區塊中關閉您的對賬單。當你寫 statement = connection.createStatement()

這是很好的做法使用後關閉語句生成

try { 
    statement = connection.createStatement(); 
    statement.execute(dataInsertQuery); 
} catch (SQLException e) { 
     e.printStackTrace(); 
} finally { 
    if (statement != null) statement.close(); 
} 
+0

謝謝,它的工作現在! – Ryainad

1

每一次新的Statement對象...

statement.close(); after `statement.execute(dataInsertQuery);` 

將解決你的問題。

0

一個額外的答案提請注意ppeterka的評論:

你真的應該在這裏使用的PreparedStatement。原因是您當前正在向數據庫發送6000個唯一的SQL插入語句。這些語句是獨一無二的,因爲插入語句的值被粘貼在語句文本中。數據庫必須解析每個唯一的語句並將其放置在其共享池中以供重用。但它不會重用,因爲它們是獨一無二的。

通過使用自己的值綁定的PreparedStatement,你就只能創建一個獨特的SQL INSERT語句,只需要解析一次,就不會弄亂共享池。你的數據庫管理員會感謝你。

+0

好的謝謝,我會搜索並嘗試。最好的祝福! – Ryainad

相關問題