0

我做了一个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();
    }
}
4

1 回答 1

0

如果您尝试执行相同的操作 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
   }
 }
}
于 2012-08-28T00:29:19.147 回答