Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Setting sql parameters for the reader

  1. #1

    Red face Setting sql parameters for the reader

    Reader is reading data from the database. In the xml file, value for the property "sql" is set for this reader. I want to include "where id=?" in the sql.

    How to set the value for this "id" in the where clause during runtime?

    I am using "JdbcCursorItemReader". Do you have any example of using "DrivingQueryItemReader"?

  2. #2

    Default

    Why donot you use JdbcDaoSupport which will enable you for preparedStatements.

    then by using setObject(1,value)..
    you can set the values...

    Regards
    Laks

  3. #3
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    That's the approach I went for on the JdbcCursorItemReader, which you can use because a PreparedStatementSetter is exposed. However, there were too many api issues for me to get it done in time for 1.0 for the DrivingQuery readers. You will need to implement your own KeyCollectors for that. It should be a pretty similar pattern to what was done for the cursor though.

  4. #4

    Exclamation

    Quote Originally Posted by lucasward View Post
    That's the approach I went for on the JdbcCursorItemReader, which you can use because a PreparedStatementSetter is exposed.
    I am using JdbcCursorItemReader. I have a class A implementing PreparedStatementSetter. The value to be set is with the JobParameters. How do i get the value from the JobParameters in the class A?

  5. #5
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    There's already a class that does that: StepExecutionPreparedStatementSetter

    You just need to register it as a StepListener as well as passing it to the item reader.

  6. #6

    Question

    Quote Originally Posted by lucasward View Post
    There's already a class that does that: StepExecutionPreparedStatementSetter

    You just need to register it as a StepListener as well as passing it to the item reader.
    Thanks, Lucas!
    One more questions.... If i need to retrieve the value from the executionContext then do i need to implement (creating a class)StepExecutionPreparedStatementSetter?

  7. #7

    Default

    Any clue how to implement this?

  8. #8
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    Yes, you would implement it in a similar way to the JobParameters version that's already available, all you would need would be to get the ExecutionContext, in addition to the JobParameters from the StepExecution.

  9. #9

    Exclamation

    Quote Originally Posted by lucasward View Post
    Yes, you would implement it in a similar way to the JobParameters version that's already available, all you would need would be to get the ExecutionContext, in addition to the JobParameters from the StepExecution.
    LoadID was stored in the executionContext by step-1. I am attaching StepExecutionPreparedStSetter to the step-2. So here in the step-2, would obviously not get the LoadID from the ExecutionContext.

    I want to keep the listeners separate for the step-1 and step-2 (reason-1 -> easier to maintain..... reason-2 -> don't want to set empty parameterKeys for step-1)

    One of the ways i was thinking was to access the LoadID from the table step_execution_context. Is there any better way to access LoadID in the step-2?

  10. #10
    Join Date
    Apr 2008
    Posts
    174

    Default

    One easy way I can think of is to define your own singleton context and place all your job/step related parameters in there.

    For ex. define MyJobContext as a singleton java class and have some properties (like LoadID) in it. you should be able to get that from your job or any subsequent steps.

    Hope that helps!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •