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

Commit

Permalink
[ch03] Subtract method. (deleteAll).
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 2, 2017
1 parent 3b97d4a commit 22c3f9b
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,23 @@ public User get(String id) throws ClassNotFoundException, SQLException {
}

public void deleteAll() throws SQLException {
Connection c = dataSource.getConnection();

PreparedStatement ps = c.prepareStatement("DELETE FROM users");
ps.executeUpdate();
Connection c = null;
PreparedStatement ps = null;

ps.close();
c.close();
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 {
Expand All @@ -102,4 +112,10 @@ public int getCount() throws SQLException {

return count;
}

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

0 comments on commit 22c3f9b

Please sign in to comment.