《java.sql.SQLException ORA-01000 超出打开游标的最大数的原因.doc》由会员分享,可在线阅读,更多相关《java.sql.SQLException ORA-01000 超出打开游标的最大数的原因.doc(3页珍藏版)》请在金锄头文库上搜索。
1、原创java.sql.SQLException: ORA-01000: 超出打开游标的最大数的原因和解决方案旧一篇:关于生成PDF里复杂数学公式的做法? 新一篇:关于Ajax返回html的解析问题 先看有问题的代码 查看复制到剪切板打印1. importjava.sql.Connection; 2. importjava.sql.DriverManager; 3. importjava.sql.SQLException; 4. importjava.sql.Statement; 5. 6. publicclassTest 7. publicConnectiongetConnection() 8
2、. Stringurl=jdbc:oracle:thin:localhost:1521:ora9i; 9. Stringuser=scott; 10. Stringpassword=tiger; 11. Connectioncon=null; 12. try 13. Class.forName(oracle.jdbc.driver.OracleDriver).newInstance(); 14. con=DriverManager.getConnection(url,user,password); 15. catch(Exceptione) 16. e.printStackTrace(); 1
3、7. 18. returncon; 19. 20. 21. publicstaticvoidmain(Stringargs)throwsSQLException 22. longa=13819100000L; 23. longb=13819100600L;/问题点 24. Connectioncon=null; 25. Statementstmt=null; 26. Testinsert=newTest(); 27. try 28. con=insert.getConnection(); 29. for(longc=a;c=b;c+) 30. Stringsql=insertintotelep
4、numvalues(+c+); 31. stmt=con.createStatement();/这里是问题的所在 32. stmt.executeUpdate(sql); 33. 34. System.out.println(OK); 35. catch(Exceptione) 36. e.printStackTrace(); 37. finally 38. if(con!=null) 39. con.close(); 40. 41. 42. 43. import java.sql.Connection;import java.sql.DriverManager;import java.sql
5、.SQLException;import java.sql.Statement;public class Test public Connection getConnection() String url = jdbc:oracle:thin:localhost:1521:ora9i; String user = scott; String password = tiger; Connection con = null; try Class.forName(oracle.jdbc.driver.OracleDriver).newInstance(); con = DriverManager.g
6、etConnection(url, user, password); catch (Exception e) e.printStackTrace(); return con; public static void main(String args) throws SQLException long a = 13819100000L; long b = 13819100600L; / 问题点 Connection con = null; Statement stmt = null; Test insert = new Test(); try con = insert.getConnection(
7、); for (long c = a; c = b; c+) String sql = insert into telepnum values( + c + ); stmt = con.createStatement(); / 这里是问题的所在 stmt.executeUpdate(sql); System.out.println(OK); catch (Exception e) e.printStackTrace(); finally if (con != null) con.close(); 分析 在循环里面每次都 stmt = con.createStatement(); 而没有释放,这
8、样每个都占用了一个服务器的游标资源,最后造成失败 解决方案 1 增加关闭语句 查看复制到剪切板打印1. con=insert.getConnection(); 2. for(longc=a;c=b;c+) 3. Stringsql=insertintotelepnumvalues(+c+); 4. stmt=con.createStatement();/这里是问题的所在 5. stmt.executeUpdate(sql); 6. stmt.close();/用完了就关闭好了 7. con = insert.getConnection(); for (long c = a; c = b; c+
9、) String sql = insert into telepnum values( + c + ); stmt = con.createStatement(); / 这里是问题的所在 stmt.executeUpdate(sql); stmt.close(); / 用完了就关闭好了 2 将这句话移动到循环外面,推荐用这个 查看复制到剪切板打印1. con=insert.getConnection(); 2. stmt=con.createStatement();/移动到这里,Statemet是可以重用的 3. for(longc=a;c=b;c+) 4. Stringsql=inserti
10、ntotelepnumvalues(+c+); 5. stmt.executeUpdate(sql); 6. 7. stmt.close();/用完了就关闭好了 con = insert.getConnection(); stmt = con.createStatement(); / 移动到这里,Statemet是可以重用的 for (long c = a; c = b; c+) String sql = insert into telepnum values( + c + ); stmt.executeUpdate(sql); stmt.close(); / 用完了就关闭好了3 改装成批量更
11、新 查看复制到剪切板打印1. con=insert.getConnection(); 2. con.setAutoCommit(false); 3. stmt=con.createStatement();/移动到这里,Statemet是可以重用的 4. for(longc=a;c=b;c+) 5. Stringsql=insertintotelepnumvalues(+c+); 6. stmt.addBatch(sql); 7. 8. stmt.executeBatch(); 9. mit(); con = insert.getConnection(); con.setAutoCommit(false); stmt = con.createStatement(); / 移动到这里,Statemet是可以重用的 for (long c = a; c = b; c+) String sql = insert into telepnum values( + c + ); stmt.addBatch(sql); stmt.executeBatch(); mit();就这么多了!