java - 如何管理数百个插入?

标签 java sql cursor database-connection

当我们无法更改游标数时,避免 ORA-0100: Maximum open cursors exceeded 的最佳方法是什么?

有没有更好的方法:

Connection connection = DriverManager.getConnection(/* Oracle Driver */);
Statement st = null;
  st = connection.createStatement();
for (/* a lot of iteration with counter */) {

  st.executeUpdate(insertSql);

  if ( counter % 500 == 0) {
    st.close();
    connection.commit();
    st = connection.createStatement();
  }
}

哪个方法调用使用游标:executeUpdate 还是 createStatement
我认为这是 executeUpdate 方法,这就是我制作此计数器的原因。

对于我工作的 Oracle:

select * from v$version;

结果:

BANNER
----------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

最佳答案

您仅在每 50 个声明中关闭...

for (/* a lot of iteration with counter */) {
  st = connection.createStatement(); //you create a statement each iteration!

  //...

  // This whole thing not right here
  if ( counter % 50 == 0) {
    st.close(); // not right here -- will have 49 open statements by now
    connection.commit(); //this too
  }

}

对于这种数据量,您应该使用准备好的语句和批量插入。

 PreparedStatement statement = connection.prepareStatement(insertTableSQL);
 for(<the objects you have>) {

   //set parameters into insert query
   statement.set*.*(1, <paramValue>);

   statement.addBatch(); //add it to the batch
 }

 statement.executeBatch();//execute whole batch
 connection.commit();

关于java - 如何管理数百个插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19276903/

相关文章:

python - MySQLdb.cursors.Cursor.execute 在不同游标的情况下返回不同的值,为什么?

java - 抽象类如何返回子类对象的ArrayList?

java - ShowOptionDialog 取消选项不起作用

java - 从多个集合中找到最大匹配计数集合

MySQL-当其中的变量不存在时获取 SUM

sql - 在一个语句中选择两个表的联接中的第一行

css - 图标字体字符上的光标更改

java - Eclipse 中有不同颜色的 System.out.println() 语句吗?

mysql - 如何从另一张 table 获取 ID 号?

objective-c - NSTextField 获得焦点但看不到文本输入光标