Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch03] Use anonymous inner class.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 2, 2017
1 parent 67b2710 commit ae4f7bf
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

import javax.sql.DataSource;

import ch03.springbook.user.dao.AddStatement;
import org.springframework.dao.EmptyResultDataAccessException;

import ch01.springbook.user.domain.User;
import ch03.springbook.user.dao.DeleteAllStatement;
import ch03.springbook.user.dao.StatementStrategy;

public class UserDao {
Expand All @@ -37,8 +35,18 @@ public void setConnectionMaker(ConnectionMaker connectionMaker) {

public void add(User user) throws ClassNotFoundException, SQLException {

StatementStrategy st = new AddStatement(user);
jdbcContextWithStatementStrategy(st);
jdbcContextWithStatementStrategy(new 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 User get(String id) throws ClassNotFoundException, SQLException {
Expand Down Expand Up @@ -72,8 +80,13 @@ public User get(String id) throws ClassNotFoundException, SQLException {

public void deleteAll() throws SQLException {

StatementStrategy st = new DeleteAllStatement();
jdbcContextWithStatementStrategy(st);
jdbcContextWithStatementStrategy(new StatementStrategy() {
@Override
public PreparedStatement makePreparedStatement(Connection c) throws SQLException {
PreparedStatement ps = c.prepareStatement("delete from users");
return ps;
}
});
}

public int getCount() throws SQLException {
Expand Down

0 comments on commit ae4f7bf

Please sign in to comment.