Here's a method that inserts a single row into a table named TRANSACTION:
Code:
private static final String INSERT_TRAN_SQL =
"insert into TRANSACTION (type, tran_date, description, maude_paid, donal_paid) values (?, ?, ?, ?, ?)";
public void insertTransaction(final Transaction tran) {
getJdbcTemplate().update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(
Connection connection) throws SQLException {
PreparedStatement ps = connection
.prepareStatement(INSERT_TRAN_SQL);
// Parameters order is: type, tran_date, description,
// maude_paid, donal_paid
ps.setString(1, tran.getTransactionType().toString());
Date sqlDate = new Date(tran.getDate().getTime());
ps.setDate(2, sqlDate);
ps.setString(3, tran.getDescription());
ps.setFloat(4, tran.getMaudePaid());
ps.setFloat(5, tran.getDonalPaid());
return ps;
}
});
}
To insert multiple rows I guess you just need to replace the INSERT...VALUES with an INSERT....SELECT. Also, you should replace the ? parameters with named parameters.
Cheers,
DM