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

Commit

Permalink
[ch03] Apply template method pattern. (deleteAll).
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 2, 2017
1 parent 22c3f9b commit fc36e6f
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/main/java/ch03/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package ch03.springbook.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;

import ch01.springbook.user.dao.ConnectionMaker;
import ch01.springbook.user.domain.User;

public abstract class UserDao {

private ConnectionMaker connectionMaker;
private DataSource dataSource;

public UserDao() {

}

public UserDao(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}

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

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

public void add(User user) throws ClassNotFoundException, SQLException {
Connection c = dataSource.getConnection();

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());

ps.executeUpdate();

ps.close();
c.close();
}

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;

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

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

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

return user;
}

public void deleteAll() throws SQLException {

Connection c = null;
PreparedStatement ps = null;

try {

c = dataSource.getConnection();
ps = makeStatement(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) {} }
}
}

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 count;
}

abstract protected PreparedStatement makeStatement(Connection c) throws SQLException;
}
14 changes: 14 additions & 0 deletions src/main/java/ch03/springbook/user/dao/UserDaoDeleteAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch03.springbook.user.dao;

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

public class UserDaoDeleteAll extends UserDao {

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

0 comments on commit fc36e6f

Please sign in to comment.