Solved

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

Posted on 2006-11-20
7
206 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now