Solved

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

Posted on 2006-11-20
7
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

622 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