File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatement.clearParameters();

 
Paul Doherty
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Baiter
Ranch Hand
Posts: 532
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Kevin Mukhar
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
Peter Tran
Bartender
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic