Kbase P28194: NullPointerException using the JDBC method PreparedStatement.clearParameters
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  8/2/2004 |
|
Status: Unverified
FACT(s) (Environment):
Progress 9.1D
FACT(s) (Environment):
OpenEdge 10.0A
SYMPTOM(s):
NullPointerException using the JDBC method PreparedStatement.clearParameters
CAUSE:
Using a PreparedStatement object for sending SQL statements to the database many times will normally reduce execution time. For an update statement by calling one of the setXXX methods defined in the class PreparedStatement it is possible to substitute a value for a question mark.
For example the following line creates the PreparedStatement:
PreparedStatement statement = con.prepareStatement("UPDATE customer SET name = ? where phone = '0420 62152'");
And the following line will substitute the value for the question mark:
statement.setString(1,"This is a test");
With this PreparedStatement there is no problem to clear the set parameters with the line:
statement.clearParameters();
However if the PreparedStatement doesn't contain any parameters and is created with the line:
PreparedStatement statement = con.prepareStatement("Select name from customer");
than since there exists no parameter objects the clearParameters() will throw a NullPointerException.
FIX:
Making sure that the clearParameters() method is only called if parameter objects exist will prevent any NullPointerException at that point. It is possible for example to set a boolean variable to allow that check within the code:
statement = con.prepareStatement("Select name from customer");
boolean params = false;
if (params) statement.clearParameters();