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

Commit

Permalink
[ch03] Use JdbcContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 2, 2017
1 parent ae4f7bf commit 370420c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
41 changes: 6 additions & 35 deletions src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,29 @@
import org.springframework.dao.EmptyResultDataAccessException;

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

public class UserDao {

private ConnectionMaker connectionMaker;
private JdbcContext jdbcContext;
private DataSource dataSource;

public UserDao() {

}

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

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

public void setConnectionMaker(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}

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

jdbcContextWithStatementStrategy(new StatementStrategy() {
this.jdbcContext.workWithStatementStrategy(new StatementStrategy() {
@Override
public PreparedStatement makePreparedStatement(Connection c) throws SQLException {
PreparedStatement ps = c.prepareStatement(
Expand Down Expand Up @@ -80,7 +77,7 @@ public User get(String id) throws ClassNotFoundException, SQLException {

public void deleteAll() throws SQLException {

jdbcContextWithStatementStrategy(new StatementStrategy() {
this.jdbcContext.workWithStatementStrategy(new StatementStrategy() {
@Override
public PreparedStatement makePreparedStatement(Connection c) throws SQLException {
PreparedStatement ps = c.prepareStatement("delete from users");
Expand All @@ -105,30 +102,4 @@ public int getCount() throws SQLException {

return count;
}

private PreparedStatement makeStatement(Connection c) throws SQLException {
PreparedStatement ps;
ps = c.prepareStatement("delete from users");
return ps;
}

public void jdbcContextWithStatementStrategy(StatementStrategy stmt) throws SQLException {

Connection c = null;
PreparedStatement ps = null;

try {

c = dataSource.getConnection();
ps = stmt.makePreparedStatement(c);
ps.executeUpdate();

} catch (SQLException e) {
throw e;

} finally {
if (ps != null) { try { ps.close(); } catch (SQLException e) {} }
if (c != null) { try { c.close(); } catch (SQLException e) {} }
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/ch03/springbook/user/dao/JdbcContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ch03.springbook.user.dao;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JdbcContext {

private DataSource dataSource;

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

public void workWithStatementStrategy(StatementStrategy stmt) throws SQLException {

Connection c = null;
PreparedStatement ps = null;

try {

c = this.dataSource.getConnection();
ps = stmt.makePreparedStatement(c);

ps.executeUpdate();

} catch (SQLException e) {
throw e;

} finally {

if (ps != null) { try { ps.close(); } catch (SQLException e) {} }
if (c != null) { try { c.close(); } catch (SQLException e) {} }
}
}
}
6 changes: 5 additions & 1 deletion src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
<property name="password" value="study" />
</bean>

<bean id="jdbcContext" class="ch03.springbook.user.dao.JdbcContext">
<property name="dataSource" ref="dataSource" />
</bean>

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

Expand Down

0 comments on commit 370420c

Please sign in to comment.