Solved

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

Posted on 2006-11-20
7
200 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes, you have to close the statement, no matter if they are used for insert, update or for getting resultset.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XSLT 5 36
Setup a configurable messaging system 3 60
API Blueprint array help 1 212
installing and using WTP plugin eclipse MARS 3 82
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…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

11 Experts available now in Live!

Get 1:1 Help Now