add() 메서드의 PreparedStatement 생성 로직을 분리한 클래스
public class AddStatement implements StatementStrategy{
@Override
public **PreparedStatement** makePreparedStatement(**Connection** c) throws SQLException {
PreparedStatement ps = c.prepareStatement("insert into users(id, name, password) values(?,?,?)");
ps.setString(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPassword());
return ps;
}
}
public class UserDao{
...
public void add(User user) throws ClassNotFoundException, SQLException {
StatementStrategy strategy = new **AddStatement(user)**;
jdbcContextWithStatementStrategy(strategy);
}
이렇게 해서 deleteAll() 과 add() 두 군데에서 모두 PreparedStatement 를 실행하는 JDBC try/catch/finally
컨텍스트를 공유해서 사용할 수 있게 되었다.