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

Commit

Permalink
[ch03] Use JdbcTemplate.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 2, 2017
1 parent 2e6c24b commit ab1e0ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 58 deletions.
97 changes: 40 additions & 57 deletions src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,92 +7,75 @@

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

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

public class UserDao {

private JdbcContext jdbcContext;
private JdbcTemplate jdbcTemplate;
private DataSource dataSource;

public UserDao() {

}

public void setJdbcContext(JdbcContext jdbcContext) {
this.jdbcContext = jdbcContext;
}

public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.dataSource = dataSource;
}

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

this.jdbcContext.workWithStatementStrategy(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;
}
});
this.jdbcTemplate.update("INSERT INTO users(id, name, password) VALUES(?, ?, ?)",
user.getId(), user.getName(), user.getPassword());
}

public User get(String id) throws ClassNotFoundException, SQLException {
Connection c = dataSource.getConnection();

PreparedStatement ps = c.prepareStatement(
"SELECT * FROM users WHERE id = ?");
ps.setString(1, id);

ResultSet rs = ps.executeQuery();

User user = null;
return this.jdbcTemplate.queryForObject(
"SELECT * FROM users WHERE id = ?",
new Object[] {id},
new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {

if (rs.next()) {
user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
}
User user = new User();
user.setId(resultSet.getString("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));

rs.close();
ps.close();
c.close();

if (user == null) {
throw new EmptyResultDataAccessException(1);
}

return user;
return user;
}
}
);
}

public void deleteAll() throws SQLException {
this.jdbcContext.executeSql("delete from users");
this.jdbcTemplate.update("delete from users");
}

public int getCount() throws SQLException {
Connection c = dataSource.getConnection();

PreparedStatement ps = c.prepareStatement("select count(*) from users");

ResultSet rs = ps.executeQuery();
rs.next();

int count = rs.getInt(1);

rs.close();
ps.close();
c.close();
return this.jdbcTemplate.query(
new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
return connection.prepareStatement("select count(*) from users");
}
},
new ResultSetExtractor<Integer>() {
@Override
public Integer extractData(ResultSet resultSet) throws SQLException, DataAccessException {
resultSet.next();
return resultSet.getInt(1);
}
}
);

return count;
// this.jdbcTemplate.queryForInt("select count(*) from users");
}
}
1 change: 0 additions & 1 deletion src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
</bean>

<bean id="userDao" class="ch01.springbook.user.dao.UserDao">
<property name="jdbcContext" ref="jdbcContext" />
<property name="dataSource" ref="dataSource" />
</bean>

Expand Down

0 comments on commit ab1e0ff

Please sign in to comment.