View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.sample.domain.football.internal;
18  
19  import java.util.List;
20  
21  import org.springframework.batch.item.ItemWriter;
22  import org.springframework.batch.sample.domain.football.Game;
23  import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
24  import org.springframework.jdbc.core.namedparam.SqlParameterSource;
25  import org.springframework.jdbc.core.support.JdbcDaoSupport;
26  import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
27  
28  public class JdbcGameDao extends JdbcDaoSupport implements ItemWriter<Game> {
29  
30  	private SimpleJdbcInsert insertGame;
31  
32  	protected void initDao() throws Exception {
33  		super.initDao();
34  		insertGame = new SimpleJdbcInsert(getDataSource()).withTableName("GAMES").usingColumns("player_id", "year_no",
35  				"team", "week", "opponent", " completes", "attempts", "passing_yards", "passing_td", "interceptions",
36  				"rushes", "rush_yards", "receptions", "receptions_yards", "total_td");
37  	}
38  
39  	public void write(List<? extends Game> games) {
40  
41  		for (Game game : games) {
42  
43  			SqlParameterSource values = new MapSqlParameterSource().addValue("player_id", game.getId()).addValue(
44  					"year_no", game.getYear()).addValue("team", game.getTeam()).addValue("week", game.getWeek())
45  					.addValue("opponent", game.getOpponent()).addValue("completes", game.getCompletes()).addValue(
46  							"attempts", game.getAttempts()).addValue("passing_yards", game.getPassingYards()).addValue(
47  							"passing_td", game.getPassingTd()).addValue("interceptions", game.getInterceptions())
48  					.addValue("rushes", game.getRushes()).addValue("rush_yards", game.getRushYards()).addValue(
49  							"receptions", game.getReceptions()).addValue("receptions_yards", game.getReceptionYards())
50  					.addValue("total_td", game.getTotalTd());
51  			this.insertGame.execute(values);
52  
53  		}
54  
55  	}
56  
57  }