• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PreparedStatement.clearParameters();

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I was wondering what the significance of using the clearParameters method of the PreparedStatement class. What exactly does it do. I have now included it in my code but prior to doing so I had no problems with not using it. Are there any cases when it is absolutely necessary?
Regards,
Paul.
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the main use of it is when you're reusing the
PreparedStatement. You can clear the parameters, then set
new ones and execute again without creating a new
PreparedStatement.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using a PreparedStatement object, the parameters you set become part of the state of the object. That is, the parameters persist between uses of a PreparedStatement instance.
For example:
    String ssql = "select name, empno from emp where " +
        "name = ? and deptno = ?"
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.setString(1, "JONES");
    ps.setInt(2, 3400);
    //select .... where name='JONES' and deptno=3400
    rset = ps.executeQuery();
If the code then changes the first parameter and executes another query, the PreparedStatement "remembers" that the second parameter is set to 3400:
    ps.setString(1, "SMITH");
    //select .... where name='SMITH' and deptno=3400
    rset = ps.executeQuery();
Here is what the Javadoc says about clearParameters():
"In general, parameter values remain in force for repeated use of a Statement. Setting a parameter value automatically clears its previous value. However, in some cases it is useful to immediately release the resources used by the current parameter values; this can be done by calling clearParameters."
So, as you stated, your code can work just fine without calling clearParameters(). I have no idea what those "some cases" are, and I've never needed to use clearParameters().
[This message has been edited by Kevin Mukhar (edited January 23, 2001).]
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin,
You should use a clearParameters() to be on the safe side. Especially, if you use setObject() method then the JDBC driver might keep some reference of your object around forever and not allow the GC to reclaim this object. Memory leak!
If you're only setting primitive values, then you don't need to execute this method.
-Peter
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic