void setValue ( int parameterIndex , PreparedStatement preparedStatement , Class < ? > fieldType , Object fieldValue ) throws Exception { try { if ( fieldType == String . class ) { preparedStatement . setString ( parameterIndex , ( String ) fieldValue ) ; } else if ( fieldType == java . sql . Timestamp . class ) { preparedStatement . setTimestamp ( parameterIndex , ( java . sql . Timestamp ) fieldValue ) ; } else if ( fieldType == java . sql . Date . class ) { preparedStatement . setDate ( parameterIndex , ( java . sql . Date ) fieldValue ) ; } else if ( fieldType == java . sql . Time . class ) { preparedStatement . setTime ( parameterIndex , ( java . sql . Time ) fieldValue ) ; } else if ( fieldType == boolean . class || fieldType == Boolean . class ) { if ( fieldValue != null ) { boolean boolValue = ( boolean ) ( Boolean ) fieldValue ; preparedStatement . setInt ( parameterIndex , ( boolValue ? 1 : 0 ) ) ; } else { preparedStatement . setInt ( parameterIndex , 0 ) ; } } else if ( fieldType == int . class || fieldType == Integer . class ) { if ( fieldValue != null ) { preparedStatement . setInt ( parameterIndex , ( int ) ( Integer ) fieldValue ) ; } else { preparedStatement . setNull ( parameterIndex , java . sql . Types . INTEGER ) ; } } else if ( fieldType == long . class || fieldType == Long . class ) { if ( fieldValue != null ) { preparedStatement . setLong ( parameterIndex , ( long ) ( Long ) fieldValue ) ; } else { preparedStatement . setNull ( parameterIndex , java . sql . Types . INTEGER ) ; } } else if ( fieldType == float . class || fieldType == Float . class ) { if ( fieldValue != null ) { preparedStatement . setFloat ( parameterIndex , ( float ) ( Float ) fieldValue ) ; } else { preparedStatement . setNull ( parameterIndex , java . sql . Types . FLOAT ) ; } } else if ( fieldType == double . class || fieldType == Double . class ) { if ( fieldValue != null ) { preparedStatement . setDouble ( parameterIndex , ( double ) ( Double ) fieldValue ) ; } else { preparedStatement . setNull ( parameterIndex , java . sql . Types . DOUBLE ) ; } } else { final String msg = "Unknown data type. type=" + fieldType + " value=" + fieldValue ; loge ( msg ) ; throw new RuntimeException ( msg ) ; } } catch ( Exception e ) { loge ( "#setValue" , e ) ; throw e ; } }
Set value to preparedstatement with appropriate cast