Aug 23rd, 2012, 09:14 AM
Query the database after having read a row of a file
I am currently reading a lot of examples and documentation but I really don't understand how to solve the following problem.
SO far, I configured Spring Batch to read data from an input file, parse and write to an output file the parsed data using a regular expression during the read phase.
But I need to query the database and get further data, before writing to the output file.
I've seen many approaches but I am still not convinced on what is the right one because, I've tried to use a processor to try to build a query to the database, or using something like JDBCTemplate with RowMapper for JdbcCursorItemReader etc.
To be honest, I don't understand if I have to implement my own ItemReader in this case or if it is sufficient to configure the xml spring batch file.
Note that, for each row of the file I am reading I get a String. Such String should be used to query the database, using a PreparedStatement (for instance), since I have something like "select name, age from person where name = ?".
Even reading this, I am still confused, because it shows examples where it uses "this.jdbcTemplate.queryForInt(...)" and I don't understand where to place such code.
Could somebody help me please?
Aug 24th, 2012, 01:52 AM
You need a processor (as I already mentioned in the other posts) and the logic to convert the string to something else should go in there (in this case your query for the database). How you do that is up to you but I would recommend using JdbcTemplate.
Aug 24th, 2012, 02:30 AM
Perfect! Thank you so much. I will use the processor then. I've actually tried to use a sort of "stub" in the processor to set the field of my object, but I didn't know if there was a better way to query the DB out of a processor.
I will let you know just in case of futher problems. Thanks.