Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

733 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