Java tutorial
/******************************************************************************* * Copyright (c) 2013 BowenCai. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html * * Contributors: * BowenCai - initial API and implementation ******************************************************************************/ package net.freechoice.model.orm; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import net.freechoice.model.FC_Transaction; import org.springframework.jdbc.core.PreparedStatementCreator; /** * * @author BowenCai * */ public class Map_Transaction implements IMapper<FC_Transaction> { @Override public FC_Transaction mapRow(final ResultSet rs, int rowNum) throws SQLException { FC_Transaction transaction = new FC_Transaction(); transaction.id = rs.getInt(1); transaction.id_account_ = rs.getInt(2); transaction.time_committed = rs.getTimestamp(3); transaction.amount = rs.getBigDecimal(4); transaction.comment = rs.getString(5); return transaction; } @Override public PreparedStatementCreator createUpdate(final FC_Transaction entity) { return new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement("update FC_Transaction" + " set id_account_ = ?," + " amount = ?, " + " comment = ? " + "where id = " + entity.id); ps.setInt(1, entity.id_account_); ps.setBigDecimal(2, entity.amount); ps.setString(3, entity.comment); return ps; } }; } @Override public PreparedStatementCreator createInsert(final FC_Transaction transaction) { return new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement( "insert into FC_Transaction(" + "id_account_, " + "amount, comment)" + " values(?, ?, ?)", RET_ID); ps.setInt(1, transaction.id_account_); ps.setBigDecimal(2, transaction.amount); ps.setString(3, transaction.comment); return ps; } }; } }