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します。PreparedStatementaddBatch()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 に答える