Cannot retrieve null String from a PreparedStatement.


General J2EE: Cannot retrieve null String from a PreparedStatement.

  1. Hello.
    If I call :

      String myStr = myStatement.getString(1);

    I get "null" (literaly 4 chars) in myStr if the value of the retrieved data is null. I try this:

      String myStr = ""+myStatement.getString(1);

    but the problem remains since literal "null" string is returned by the statement object.

    Have you got an idea on how to solve it? I would like retrieving the null value but not the "null" string.

    I use Sun's j2ee reference implementation 1.2.1 + jdk1.2

    J. Carlos Muro
  2. First off be sure that the field in the Table/row you are querying does not actually contain "null" in that column.

    Its quite common if you did an insert from somewhere in your Java class via JDBC that upon String conversion of the query the null was evaluated as "null". Use a vendors tool to look at the row in question and verify the value.

    I would seriously doubt that the JDBC driver has the bug because most low level protocols would send back some sort of string length indicator. But who knows for sure.
  3. Your problem is with this statement.

    String myStr = ""+myStatement.getString(1);

    The driver returns a null object but you are converting it explicitly to "null" string.

    statement.getString(int) returns a string. remove the double quotes from the beginning.