private CommandOutput < ? , ? > executePreparedSql ( List < Command > cmds ) { String query = null ; List < String > queryParameters = null ; int fetchSize = 0 ; for ( Command cmd : cmds ) { if ( cmd instanceof JdbcCommand ) { JdbcCommandType type = ( JdbcCommandType ) cmd . getCommandType ( ) ; switch ( type ) { case QUERY : query = cmd . getParams ( ) . get ( 0 ) ; break ; case QUERYPARAMS : queryParameters = cmd . getParams ( ) ; break ; case FETCHSIZE : fetchSize = Integer . parseInt ( cmd . getParams ( ) . get ( 0 ) ) ; break ; default : this . log . error ( "Command " + type . toString ( ) + " not recognized" ) ; break ; } } } this . log . info ( "Executing query:" + query ) ; ResultSet resultSet = null ; try { this . jdbcSource = createJdbcSource ( ) ; if ( this . dataConnection == null ) { this . dataConnection = this . jdbcSource . getConnection ( ) ; } PreparedStatement statement = this . dataConnection . prepareStatement ( query , ResultSet . TYPE_FORWARD_ONLY , ResultSet . CONCUR_READ_ONLY ) ; int parameterPosition = 1 ; if ( queryParameters != null && queryParameters . size ( ) > 0 ) { for ( String parameter : queryParameters ) { statement . setString ( parameterPosition , parameter ) ; parameterPosition ++ ; } } if ( fetchSize != 0 ) { statement . setFetchSize ( fetchSize ) ; } final boolean status = statement . execute ( ) ; if ( status == false ) { this . log . error ( "Failed to execute sql:" + query ) ; } resultSet = statement . getResultSet ( ) ; } catch ( Exception e ) { this . log . error ( "Failed to execute sql:" + query + " ;error-" + e . getMessage ( ) , e ) ; } CommandOutput < JdbcCommand , ResultSet > output = new JdbcCommandOutput ( ) ; output . put ( ( JdbcCommand ) cmds . get ( 0 ) , resultSet ) ; return output ; }
Execute query using JDBC PreparedStatement to pass query parameters Set fetch size