Skip to content

Commit

Permalink
JDBC 구현slipp#5
Browse files Browse the repository at this point in the history
  • Loading branch information
funchcode committed Mar 21, 2019
1 parent 92b9427 commit 26fbe6d
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/main/java/next/controller/CreateUserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import core.db.DataBase;
import core.mvc.Controller;
import next.dao.UserDao;
import next.model.User;

public class CreateUserController implements Controller {
Expand All @@ -17,6 +18,7 @@ public class CreateUserController implements Controller {
public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
User user = new User(req.getParameter("userId"), req.getParameter("password"), req.getParameter("name"),
req.getParameter("email"));

log.debug("User : {}", user);

DataBase.addUser(user);
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/next/dao/InsertJdbcTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package next.dao;

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

import core.jdbc.ConnectionManager;
import next.model.User;

public abstract class InsertJdbcTemplate {
void insert(User user) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(createQueryForInsert());
setValuesForInsert(user, pstmt);
pstmt.executeUpdate();
} finally {
if (pstmt != null) {
pstmt.close();
}

if (con != null) {
con.close();
}
}
}

abstract void setValuesForInsert(User user, PreparedStatement pstmt) throws SQLException;
abstract String createQueryForInsert();
}
31 changes: 31 additions & 0 deletions src/main/java/next/dao/JdbcTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package next.dao;

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

import core.jdbc.ConnectionManager;
import next.model.User;

public abstract class JdbcTemplate {
void update(String query) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(query);
setValues(pstmt);
pstmt.executeUpdate();
} finally {
if (pstmt != null) {
pstmt.close();
}

if (con != null) {
con.close();
}
}
}

abstract void setValues(PreparedStatement pstmt) throws SQLException;
}
32 changes: 32 additions & 0 deletions src/main/java/next/dao/UpdateJdbcTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package next.dao;

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

import core.jdbc.ConnectionManager;
import next.model.User;

public abstract class UpdateJdbcTemplate {
static void update(User user, UserDao userDao) throws SQLException{
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(userDao.createQueryForUpdate());
userDao.setValuesForUpdate(user, pstmt);
pstmt.executeUpdate();
} finally {
if (pstmt != null) {
pstmt.close();
}

if (con != null) {
con.close();
}
}
}

abstract void setValuesForUpdate(User user, PreparedStatement pstmt);
abstract String createQueryUpdate();
}
86 changes: 63 additions & 23 deletions src/main/java/next/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,77 @@
import next.model.User;

public class UserDao {

void setValuesForInsert(User user, PreparedStatement pstmt) throws SQLException {
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getName());
pstmt.setString(4, user.getEmail());
}

void setValuesForUpdate(User user, PreparedStatement pstmt) throws SQLException {
pstmt.setString(4, user.getUserId());
pstmt.setString(1, user.getPassword());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getEmail());
}

String createQueryForInsert() {
return "INSERT INTO USERS VALUES (?,?,?,?)";
}

String createQueryForUpdate() {
return "UPDATE USERS SET password = ?, name = ?, email = ? WHERE userid = ?";
}

public void insert(User user) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
String sql = "INSERT INTO USERS VALUES (?, ?, ?, ?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getName());
pstmt.setString(4, user.getEmail());

pstmt.executeUpdate();
} finally {
if (pstmt != null) {
pstmt.close();
}

if (con != null) {
con.close();
}
}
String query = "INSERT INTO USERS VALUES (?,?,?,?)";
JdbcTemplate insertJdbc = new JdbcTemplate() {
@Override
void setValues(PreparedStatement pstmt) throws SQLException {
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getName());
pstmt.setString(4, user.getEmail());
}
};
insertJdbc.update(query);
}

public void update(User user) throws SQLException {
// TODO 구현 필요함.
UpdateJdbcTemplate.update(user, new UserDao());
}

public List<User> findAll() throws SQLException {
// TODO 구현 필요함.
return new ArrayList<User>();
ArrayList<User> list = new ArrayList<User>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
con = ConnectionManager.getConnection();
String sql = "SELECT userId, password, name, email FROM USERS";
pstmt = con.prepareStatement(sql);

rs = pstmt.executeQuery();

while(rs.next()) {
list.add(new User(rs.getString("userId"), rs.getString("password"), rs.getString("name"), rs.getString("email")));
}

return list;
} finally {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
}
}

public User findByUserId(String userId) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ public void contextInitialized(ServletContextEvent sce) {

@Override
public void contextDestroyed(ServletContextEvent sce) {

}
}
}
15 changes: 10 additions & 5 deletions src/test/java/next/dao/UserDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package next.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.List;

Expand Down Expand Up @@ -28,11 +29,15 @@ public void crud() throws Exception {
userDao.insert(expected);
User actual = userDao.findByUserId(expected.getUserId());
assertEquals(expected, actual);

expected.update(new User("userId", "password2", "name2", "[email protected]"));
userDao.update(expected);
actual = userDao.findByUserId(expected.getUserId());
assertEquals(expected, actual);
//userDao.update(new User("changePW", "changeNM", "[email protected]", "userId"));
//actual = userDao.findByUserId(expected.getUserId());
//assertEquals(expected, actual);
//assertNotEquals(expected, actual);

// expected.update(new User("userId", "password2", "name2", "[email protected]"));
// userDao.update(expected);
// actual = userDao.findByUserId(expected.getUserId());
// assertEquals(expected, actual);
}

@Test
Expand Down

0 comments on commit 26fbe6d

Please sign in to comment.