无法解决错误-java.sql.SQLException:ORA-01000:超出了最大打开游标数
我制作了一个java方法来向数据库添加一行。出于测试目的,我调用这个方法大约1000多次。我在准备好的语句中调用了close()方法,每当调用该方法插入一行时,仍然会出现oracle错误 错误 源代码无法解决错误-java.sql.SQLException:ORA-01000:超出了最大打开游标数,java,jdbc,oracle11g,ora-01000,Java,Jdbc,Oracle11g,Ora 01000,我制作了一个java方法来向数据库添加一行。出于测试目的,我调用这个方法大约1000多次。我在准备好的语句中调用了close()方法,每当调用该方法插入一行时,仍然会出现oracle错误 错误 源代码 public void insertARow(ArrayList<String> row) { try { //Proper SQL statement here, checked by running on DB String in
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();
}
}
public void插入行(ArrayList行)
{
尝试
{
//正确的SQL语句,在DB上运行检查
String insert=“插入用户信息(cola、colb)值(?,)”;
//添加一行
PreparedStatement ps=con.prepareStatement(insert);//con是连接对象
//“行”是字符串的数组列表
对于(int i=0;i
如果您尝试执行相同的操作1000次,我建议重新使用相同的PreparedStatement
或使用and组合
如果您计划重复使用您的预处理声明,以下是您可以做的事情:
public void insertARow(PreparedStatement ps,ArrayList行){
//你的代码
}
public void calledMethod(){
String insert=“插入用户信息(cola、colb)值(?,)”;
PreparedStatement ps=null;
试一试{
ps=合同准备声明(插入);
/**
*在这里,您可以调用InsertRow,并将您需要的preparedstatement传递给它
*已创建。在您的情况下,此将被调用多次。
*/
插入行(ps,行);
}最后{
如果(ps!=null){
//关闭ps
}
}
}
为什么不重用PreparedStatement
?在哪里创建连接,在哪里关闭连接?发布使用此方法的周围代码可能会有所帮助。您的PreparedStatement
有两个参数,但是您将它们错误地设置为well@Sujay-为什么不正确?@veer-在每段代码中只使用一条准备好的语句总是好的吗?
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();
}
}
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
}
}
}