日期:2014-05-17  浏览次数:20891 次

通过代码循环往数据库中插入数据时报错,求大神指教
java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 出现错误
ORA-01000: 超出打开游标的最大数
ORA-00604: 递归 SQL 级别 1 出现错误
ORA-01000: 超出打开游标的最大数
ORA-01000: 超出打开游标的最大数

外部文件里有392条数据,结果数据库中只插入了299条数据

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Address {

public static void main(String[] args)
{
String dirverClassName = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String name = "scott";
String password = "SYS_xyz123";

Connection connection = null;
try {
Class.forName(dirverClassName);

connection = DriverManager.getConnection(url, name, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

FileReader file = null;
BufferedReader buffer = null;
PreparedStatement ps = null;
try {
file = new FileReader("H:/项目资料/address.txt");
buffer = new BufferedReader(file);
String str = "";
while((str = buffer.readLine()) != null)
{
ps = connection.prepareStatement(str);
ps.executeUpdate();
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ps.close();
buffer.close();
file.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

------解决方案--------------------
写法很有问题,在while循环里不断使用PreparedStatement,而在while循环完之后才关闭资源,必然爆掉的。
改成没使用一次之后,及时close吧