Solved

Close for PreparedStatement, is it required even if the PreparedStatement has only updates

Posted on 2006-11-20
7
211 Views
Last Modified: 2010-04-06
If the PreparedStatement has only update/insert statements, then also do we have to close the statement?

As cursor are not associated with PreparedStatement. Code snippet below:

for(int i =0; i<100; i++) {
      stmt = conn.prepareStatement("insert into test values (?)");
      for(j=0;j<100;j++) {
            stmt.setString(1, "AA"+j);
            stmt.addBatch();
      }
      stmt.executeBatch();
}

Would this create any kind of problem while executing?

Appreciate your quick response!
0
Comment
Question by:acis_team
  • 2
7 Comments
 
LVL 1

Expert Comment

by:achillecarsten
ID: 18033292
It is generally poor practice to create this many connections. This can overload the server.
Thus you only prepare the statement once, ie::

stmt = conn.prepareStatement("insert into test values (?)");

for(int i =0; i<100; i++) {
     for(j=0;j<100;j++) {
          stmt.setString(1, "AA"+j);
          stmt.addBatch();
     }
     stmt.executeBatch();
}
0
 
LVL 1

Expert Comment

by:achillecarsten
ID: 18033296
Or maybe even:

stmt = conn.prepareStatement("insert into test values (?)");

for(int i =0; i<100; i++) {
     for(j=0;j<100;j++) {
          stmt.setString(1, "AA"+j);
          stmt.addBatch();
     }
}

stmt.executeBatch();
0
 

Author Comment

by:acis_team
ID: 18033538
Though what your are saying is right. The question was slightly different.
Let me rephrase it:
When we open too many statements without closing them, then usually when connecting to ORacle, you would get an error "Max Cursors exceeded". the question was, that does this argument holds good for statements that only do update and don't have resultset associated with it?

Hope this helps!
0
 
LVL 6

Accepted Solution

by:
avinthm earned 20 total points
ID: 18092389
Yes, you have to close the statement, no matter if they are used for insert, update or for getting resultset.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question