devxlogo

Avoid SQL Exceptions when Executing the Same Query Multiple Times

Avoid SQL Exceptions when Executing the Same Query Multiple Times

When you execute the same SQL query over a hundred times, you probably get exceptions similar to this one:

java.sql.SQLException: ORA-00604: error occurred at recursive SQL level 1 ORA-01000: maximum open cursors exceeded

The following code shows one reason you may be getting this exception. As you can see, the same PreparedStatement/Statement is being used after each SQL query execution, and the statement is never closed:

PreparedStatement pstmt = null...for (long i=0; i<600000; i++){ pstmt = connection.prepareStatement(...); pstmt.executeUpdate();}

In order to avoid this, make the following modifications to the code:

PreparedStatement pstmt = null...for (long i=0; i<600000; i++){ pstmt = connection.prepareStatement(...); pstmt.executeUpdate(); pstmt.close();}
See also  5 Tips to Manage a Remote Workforce
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist