Issue Details (XML | Word | Printable)

Key: DBUTILS-14
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: john gant
Votes: 0
Watchers: 0
Operations

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

[dbutils] fillPreparedStatement

Created: 08/Feb/06 01:08 AM   Updated: 07/Mar/09 06:01 AM
Component/s: None
Affects Version/s: 1.0
Fix Version/s: None

Time Tracking:
Not Specified

Environment:
Operating System: other
Platform: Other

Bugzilla Id: 38556
Resolution Date: 25/Feb/09 10:05 AM


 Description  « Hide
I am currently using release 1.0 and ran across bug addressed by dgraham
(http://svn.apache.org/viewcvs.cgi/jakarta/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/QueryRunner.java?rev=141728&view=markup).
I have a section of code I wrote as a workaround. Although this is just a simple
method, it has the internals needed to more definitively insert null values.
This method is not complete for all types, but was complete enough for my use.
private void fillPreparedStatement(PreparedStatement ps, Object value, Class
valueType, int position)
            throws SQLException, InvalidObjectException {
        if (valueType == null) {
            throw new InvalidObjectException("Type cannot be null at the same
time value is null");
        }
        else {
            if (valueType.equals(Double.class)) {
                if (value != null) {
                    ps.setDouble(position, ((Double) value).doubleValue());
                }
                else {
                    ps.setNull(position, Types.NUMERIC);
                }
            }
            else if (valueType.equals(Integer.class)) {
                if (value != null) {
                    ps.setInt(position, ((Integer) value).intValue());
                }
                else {
                    ps.setNull(position, Types.NUMERIC);
                }
            }
            else if (valueType.equals(Long.class)) {
                if (value != null) {
                    ps.setLong(position, ((Long) value).longValue());
                }
                else {
                    ps.setNull(position, Types.NUMERIC);
                }
            }
            else if (valueType.equals(Float.class)) {
                if (value != null) {
                    ps.setFloat(position, ((Float) value).floatValue());
                }
                else {
                    ps.setNull(position, Types.NUMERIC);
                }
            }
            else if (valueType.equals(String.class)) {
                if (value != null) {
                    ps.setString(position, (String) value);
                }
                else {
                    ps.setNull(position, Types.VARCHAR);
                }
            }
            else if (valueType.equals(Timestamp.class)) {
                if (value != null) {
                    ps.setTimestamp(position, (Timestamp) value);
                }
                else {
                    ps.setNull(position, Types.TIMESTAMP);
                }
            }
        }

Obviously the preparedStatement is passed into the method, along with arrays
containing the value, their intential types (as class objects) and the position
in the preparedStatement. I am sure this can be adapted easily to accomodate all
types and in the proper form. I understand this may be inactive at the moment,
but I'm just submitting a suggestion for the future.

Thanks,
John Gant



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Henri Yandell added a comment - 19/Jul/06 06:38 AM
Did you find that VARCHAR didn't work for a database?

The problem with your method is that the API doesn't have any knowledge of the Class valueType parameter.


Henri Yandell added a comment - 25/Feb/09 10:05 AM
svn ci -m "Merging in Dab Fabulich's work on https://svn.apache.org/repos/asf/commons/sandbox/dbutils/bugfixing from -r741987:747723. Resolving DBUTILS-34 - DBUTILS-37 - DBUTILS-29 - DBUTILS-14 - DBUTILS-31 - DBUTILS-39 - DBUTILS-41 - DBUTILS-44 - DBUTILS-33 - DBUTILS-42 - DBUTILS-40"

Sending pom.xml
Sending src/java/org/apache/commons/dbutils/BasicRowProcessor.java
Sending src/java/org/apache/commons/dbutils/BeanProcessor.java
Sending src/java/org/apache/commons/dbutils/QueryRunner.java
Adding src/java/org/apache/commons/dbutils/handlers/AbstractListHandler.java
Sending src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java
Sending src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java
Sending src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java
Deleting src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java
Sending src/java/org/apache/commons/dbutils/handlers/MapListHandler.java
Sending src/test/org/apache/commons/dbutils/BaseTestCase.java
Adding src/test/org/apache/commons/dbutils/QueryRunnerTest.java
Transmitting file data .........
Committed revision 747724.


Dan Fabulich added a comment - 07/Mar/09 06:01 AM
Removing "Fix Version: 1.2" since this is really a dupe; it clutters the release notes.