简体   繁体   中英

DB2 jdbc SQL Error: SQLCODE=-302, SQLSTATE=22001 on Select

There is a select query that I am executing with DB2 JDBC. I am using Prepared Statement to pass in the value for the parameter. The column length of that parameter in the database is 12 so everything works fine until the length of the value is 12 and then it fails. Throws an exception with the error message as in the title. I did some searching and found an explanation in the following link http://www-01.ibm.com/support/docview.wss?uid=swg21319477 and the resolution mentioned in there is as below

Resolving the problem Add additional client side validation code to prevents queries, with values that are larger than the allowed maximum length to be ran.

I don't want to do this. Why wouldn't the query just return back with no results. Any idea how do I go about this?

EDIT

String sql = "select student_id, student_name from student where student_id = ?";

try (Connection connection = DBUtils.GetConnection)
{
    try (PreparedStatement statement = connection.prepareStatement(sql))
    {
        statement.setString(1, student_id);

        ResultSet result = statement.executeQuery();

        while (result.next())
        {
            //...
        }
    }
}

Even though I do not recommend doing it: We had a similar problem and found that - at least in our case -, if you really want that empty result, you can use a native SQL query instead of the prepared statement. Apparently, it is the argument binding for the prepared statement which runs into the argument validation. The native query (which you would have manually constructed using your arguments) seemed to sidestep this validation, and just returned an empty result.

(For completeness' sake: If really manually constructing your SQL query from given arguments, be sure to know what you are doing, validate your arguments, and specifically beware of SQL injection.)

I am going to answer your question:

Why wouldn't the query just return back with no results. Any idea how do I go about this?

Computer programs including database management systems use errors and warnings to indicate that something went wrong, was used in a wrong way or that situations with possible conflicts or "danger" were encountered. In your case you are trying to call a defined API with a value that does not fit. The program refuses to accept that value and points out the wrong call by returning an error code.

If it would return no results, how would you know that the API call, its usage, is wrong? Now you know and can fix it. Or using exception handling ignore it and move on...

The correct answer here would be what @Gaius Gracchus offers up as an alternative suggestion in his comment to @Hans's answer. You try/catch the SQLException, gather its SQL State (always better than an SQL Code), and handle/throw a custom exception to indicate invalid input to the client. An empty result set (even though that is what the OP desires) is not accurate. The only other real alternative is to increase the size of the column or procedural input/input-output (not likely).

try {
    // sql bind or execute
}
catch (SQLException e) {
    String sqlState = e.getSQLState();
    if (sqlState != null && sqlState.equals("22001")) {
        throw new CustomException("Invalid input, etc");
    }
    throw e;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM