public PreparedStatement createPreparedStatement(ControlBeanContext context, Connection connection,
                                                     Calendar calendar, Method method, Object[] arguments)
            throws SQLException {

        PreparedStatement preparedStatement = null;
        loadSQLAnnotationStatmentOptions(context, method);
        checkJdbcSupport(connection.getMetaData());

        _callableStatement = setCallableStatement(arguments);

        try {
            final String sql = getPreparedStatementText(context, method, arguments);

            //
            // is this a request for generatedKeys ?
            //
            if (_getGeneratedKeys) {

                if (_callableStatement) {
                    throw new ControlException("getGeneratedKeys not supported for CallableStatements");
                }

                if (_genKeyColumnNames.length > 0) {
                    preparedStatement = connection.prepareStatement(sql, _genKeyColumnNames);
                } else if (_genKeyColumnIndexes.length > 0) {
                    preparedStatement = connection.prepareStatement(sql, _genKeyColumnIndexes);
                } else {
                    preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                }

            } else {

                if (_holdability == JdbcControl.HoldabilityType.DRIVER_DEFAULT) {
                    if (_scrollType == JdbcControl.ScrollType.DRIVER_DEFAULT) {
                        preparedStatement = (_callableStatement) ? connection.prepareCall(sql) : connection.prepareStatement(sql);
                    } else {
                        preparedStatement = (_callableStatement)
                                ? connection.prepareCall(sql, _scrollType.getType(), _scrollType.getConcurrencyType())
                                : connection.prepareStatement(sql, _scrollType.getType(), _scrollType.getConcurrencyType());
                    }
                } else {
                    preparedStatement = (_callableStatement)
                            ? connection.prepareCall(sql, _scrollType.getType(), _scrollType.getConcurrencyType(), _holdability.getHoldability())
                            : connection.prepareStatement(sql, _scrollType.getType(), _scrollType.getConcurrencyType(), _holdability.getHoldability());
                }
            }

            //
            // If the method argument is of type SQLParameter, treat this statement as a CallableStatement,
            //
            if (_callableStatement) {
                for (SqlFragment sf : _children) {
                    if (sf.hasParamValue()) {
                        throw new ControlException("Cannot use parameter substution and SQLParameter array in the same method.");
                    }
                }
                JdbcControl.SQLParameter[] params = (JdbcControl.SQLParameter[]) arguments[0];
                if (params == null) {
                    return preparedStatement;
                }
                for (int i = 0; i < params.length; i++) {
                    JdbcControl.SQLParameter p = params[i];
                    if (p.dir != JdbcControl.SQLParameter.OUT) {
                        Object value = params[i].value;
                        setPreparedStatementParameter(preparedStatement, i + 1, value, params[i].type, calendar);
                    }

                    if (p.dir != JdbcControl.SQLParameter.IN) {
                        ((CallableStatement) preparedStatement).registerOutParameter(i + 1, params[i].type);
                    }
                }


                //
                // special handling for batch updates
                //
            } else if (_batchUpdate) {
                doBatchUpdate(preparedStatement, arguments, calendar);


                //
                // standard case, not a batch or callable
                //
            } else {
                int pIndex = 1;
                for (SqlFragment sf : _children) {
                    if (sf.hasParamValue()) {
                        Object values[] = sf.getParameterValues(context, method, arguments);
                        for (Object value : values) {
                            setPreparedStatementParameter(preparedStatement, pIndex++, value, sf.getParamSqlDataType(), calendar);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            if (preparedStatement != null) preparedStatement.close();
            throw e;
        }

        preparedStatement.setFetchDirection(_fetchDirection.getDirection());
        preparedStatement.setFetchSize(_fetchSize);
        preparedStatement.setMaxRows(computeMaxRows(method));

        return preparedStatement;
    }
public PreparedStatement createPreparedStatement ( ControlBeanContext context , Connection connection , Calendar calendar , Method method , Object [ ] arguments ) throws SQLException { PreparedStatement preparedStatement = null ; loadSQLAnnotationStatmentOptions ( context , method ) ; checkJdbcSupport ( connection . getMetaData ( ) ) ; _callableStatement = setCallableStatement ( arguments ) ; try { final String sql = getPreparedStatementText ( context , method , arguments ) ; if ( _getGeneratedKeys ) { if ( _callableStatement ) { throw new ControlException ( "getGeneratedKeys not supported for CallableStatements" ) ; } if ( _genKeyColumnNames . length > 0 ) { preparedStatement = connection . prepareStatement ( sql , _genKeyColumnNames ) ; } else if ( _genKeyColumnIndexes . length > 0 ) { preparedStatement = connection . prepareStatement ( sql , _genKeyColumnIndexes ) ; } else { preparedStatement = connection . prepareStatement ( sql , Statement . RETURN_GENERATED_KEYS ) ; } } else { if ( _holdability == JdbcControl . HoldabilityType . DRIVER_DEFAULT ) { if ( _scrollType == JdbcControl . ScrollType . DRIVER_DEFAULT ) { preparedStatement = ( _callableStatement ) ? connection . prepareCall ( sql ) : connection . prepareStatement ( sql ) ; } else { preparedStatement = ( _callableStatement ) ? connection . prepareCall ( sql , _scrollType . getType ( ) , _scrollType . getConcurrencyType ( ) ) : connection . prepareStatement ( sql , _scrollType . getType ( ) , _scrollType . getConcurrencyType ( ) ) ; } } else { preparedStatement = ( _callableStatement ) ? connection . prepareCall ( sql , _scrollType . getType ( ) , _scrollType . getConcurrencyType ( ) , _holdability . getHoldability ( ) ) : connection . prepareStatement ( sql , _scrollType . getType ( ) , _scrollType . getConcurrencyType ( ) , _holdability . getHoldability ( ) ) ; } } if ( _callableStatement ) { for ( SqlFragment sf : _children ) { if ( sf . hasParamValue ( ) ) { throw new ControlException ( "Cannot use parameter substution and SQLParameter array in the same method." ) ; } } JdbcControl . SQLParameter [ ] params = ( JdbcControl . SQLParameter [ ] ) arguments [ 0 ] ; if ( params == null ) { return preparedStatement ; } for ( int i = 0 ; i < params . length ; i ++ ) { JdbcControl . SQLParameter p = params [ i ] ; if ( p . dir != JdbcControl . SQLParameter . OUT ) { Object value = params [ i ] . value ; setPreparedStatementParameter ( preparedStatement , i + 1 , value , params [ i ] . type , calendar ) ; } if ( p . dir != JdbcControl . SQLParameter . IN ) { ( ( CallableStatement ) preparedStatement ) . registerOutParameter ( i + 1 , params [ i ] . type ) ; } } } else if ( _batchUpdate ) { doBatchUpdate ( preparedStatement , arguments , calendar ) ; } else { int pIndex = 1 ; for ( SqlFragment sf : _children ) { if ( sf . hasParamValue ( ) ) { Object values [ ] = sf . getParameterValues ( context , method , arguments ) ; for ( Object value : values ) { setPreparedStatementParameter ( preparedStatement , pIndex ++ , value , sf . getParamSqlDataType ( ) , calendar ) ; } } } } } catch ( SQLException e ) { if ( preparedStatement != null ) preparedStatement . close ( ) ; throw e ; } preparedStatement . setFetchDirection ( _fetchDirection . getDirection ( ) ) ; preparedStatement . setFetchSize ( _fetchSize ) ; preparedStatement . setMaxRows ( computeMaxRows ( method ) ) ; return preparedStatement ; }
Generates the PreparedStatement the SQL statement.

@param context    ControlBeanContext instance.
@param connection Connection to database.
@param calendar   Calendar instance which can be used to resolve date/time values.
@param method     Method the SQL is associated with.
@param arguments  Method parameters.
@return The PreparedStatement generated by this statement.
@throws SQLException If PreparedStatement cannot be created.