无法解决错误-java.sql.sql异常:ORA-01000:超过了打开游标的最大值



我制作了一个向数据库添加行的java方法。出于测试目的,我调用这个方法大约1000多次。我在准备好的语句中调用了close()方法,每当调用该方法插入一行时,我仍然会收到oracle错误。

错误

ORA-01000: maximum open cursors exceeded

源代码

public void insertARow(ArrayList<String> row)
{
    try
    {
        //Proper SQL statement here, checked by running on DB  
        String insert = "INSERT INTO user.info(cola,colb) values(?,?)";
        //Add a row 
        PreparedStatement ps = con.prepareStatement(insert);//con is a connection object 
        //'row' is an arraylist of strings
        for(int i = 0; i < row.size(); i++ )
        {
            int j = 1 +  i ; 
            String temp = row.get(i);
            ps.setString(j , temp);
        }
        ps.executeUpdate();//The reason for problems !!!
        ps.close();
    }catch(SQLException e)
    {
        System.out.println("Cannot add row !");
        e.printStackTrace();
    }
}

如果您尝试执行相同的操作1000次,我建议re-using使用相同的PreparedStatement或使用addBatch()executeBatch()组合。

如果您计划重复使用PreparedStatement,以下是您可以做的事情:

public void insertARow(PreparedStatement ps, ArrayList<String> row){
 //your code
}
public void calledMethod(){
 String insert = "INSERT INTO user.info(cola,colb) values(?,?)";
 PreparedStatement ps = null;
 try{
   ps = con.prepareStatement(insert);
   /**
    * Here you make the call to insertARow passing it the preparedstatement that you
    * have created. This in your case will be called multiple times.
    */
   insertARow(ps, row);
 }finally{
   if(ps != null){
     //close ps
   }
 }
}

相关内容

最新更新