This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ch03] Apply template method pattern. (deleteAll).
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/ch03/springbook/user/dao/UserDaoDeleteAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |