Issue Details (XML | Word | Printable)

Key: DBUTILS-31
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Dan Fabulich
Reporter: Francis Townsend
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Commons DbUtils

fillStatement setNull bug with the Derby JDBC driver

Created: 13/Jun/06 07:45 AM   Updated: 07/Mar/09 06:09 AM
Return to search
Component/s: None
Affects Version/s: 1.0
Fix Version/s: 1.2

Time Tracking:
Not Specified

Environment: Derby 10.1.2.1

Resolution Date: 25/Feb/09 10:05 AM


 Description  « Hide
This has been documented many times before, but I was not happy with the existing code fixes. The following small code snippet should fix it for all conforming JDBC drivers.
protected void fillStatement(PreparedStatement stmt, Object[] params)
        throws SQLException {

        if (params == null) {
            return;
        }
        ParameterMetaData pmd = stmt.getParameterMetaData();
        for (int i = 0; i < params.length; i++) {
            if (params[i] != null) {
                stmt.setObject(i + 1, params[i]);
            } else {
                stmt.setNull(i + 1, pmd.getParameterType(i + 1));
            }
        }
    }

The only difference is that you get the parameter meta data and pass that type information to the setNull method. This should neatly fix this problem, with a very slight additional overhead.



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Henri Yandell made changes - 06/Jul/06 01:40 PM
Field Original Value New Value
Fix Version/s 1.1 [ 12311973 ]
Henri Yandell made changes - 02/Nov/06 06:18 AM
Fix Version/s 1.1 [ 12311973 ]
Dennis Lundberg made changes - 16/Mar/08 09:35 PM
Description This has been documented many times before, but I was not happy with the existing code fixes. The following small code snippet should fix it for all conforming JDBC drivers.

    protected void fillStatement(PreparedStatement stmt, Object[] params)
        throws SQLException {

        if (params == null) {
            return;
        }
        ParameterMetaData pmd = stmt.getParameterMetaData();
        for (int i = 0; i < params.length; i++) {
            if (params[i] != null) {
                stmt.setObject(i + 1, params[i]);
            } else {
                stmt.setNull(i + 1, pmd.getParameterType(i + 1));
            }
        }
    }

The only difference is that you get the parameter meta data and pass that type information to the setNull method. This should neatly fix this problem, with a very slight additional overhead.
This has been documented many times before, but I was not happy with the existing code fixes. The following small code snippet should fix it for all conforming JDBC drivers.

{code}
    protected void fillStatement(PreparedStatement stmt, Object[] params)
        throws SQLException {

        if (params == null) {
            return;
        }
        ParameterMetaData pmd = stmt.getParameterMetaData();
        for (int i = 0; i < params.length; i++) {
            if (params[i] != null) {
                stmt.setObject(i + 1, params[i]);
            } else {
                stmt.setNull(i + 1, pmd.getParameterType(i + 1));
            }
        }
    }
{code}

The only difference is that you get the parameter meta data and pass that type information to the setNull method. This should neatly fix this problem, with a very slight additional overhead.
Henri Yandell made changes - 25/Feb/09 10:05 AM
Resolution Fixed [ 1 ]
Fix Version/s 1.2 [ 12312139 ]
Status Open [ 1 ] Closed [ 6 ]
Dan Fabulich made changes - 07/Mar/09 06:09 AM
Assignee Dan Fabulich [ dfabulich ]