我创建了一个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();
}
}
答案 0 :(得分: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
}
}
}