2013-10-04 30 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

相关问题