-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
5주차 미션 / 서버 3조 이윤희 #52
Open
lylylylh
wants to merge
7
commits into
Konkuk-KUIT:step3-jdbc-start
Choose a base branch
from
lylylylh:step3-jdbc-start
base: step3-jdbc-start
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
acf5e17
5주차 강의 내용
lylylylh 8db4800
Controller에 UserDao 적용
lylylylh 496666c
Questions Dao
lylylylh a8db2a4
Create qna form
lylylylh 197a739
show.jsp
lylylylh 4b23742
update qna, 질문 생성 오류 수정
lylylylh 929d64b
중복 코드 제거
lylylylh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
package core.jdbc; | ||
|
||
import jwp.model.User; | ||
|
||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class JdbcTemplate<T> { | ||
|
||
// 각 method에서 parameter에 맞춰 다른 동작을 수행하도록 해줌 (동작 parameter 화) | ||
public void update(String sql, PreparedStatementSetter pstmtSetter) throws SQLException { | ||
try (Connection conn = ConnectionManager.getConnection(); | ||
PreparedStatement pstmt = conn.prepareStatement(sql)) { | ||
pstmtSetter.setParameters(pstmt); | ||
pstmt.executeUpdate(); | ||
} | ||
} | ||
|
||
public void update(String sql, PreparedStatementSetter pstmtSetter, KeyHolder holder) { | ||
try (Connection conn = ConnectionManager.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { | ||
pstmtSetter.setParameters(pstmt); | ||
pstmt.executeUpdate(); | ||
|
||
ResultSet rs = pstmt.getGeneratedKeys(); | ||
if (rs.next()) { | ||
holder.setId((int) rs.getLong(1)); | ||
} | ||
rs.close(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws SQLException { | ||
List<T> objects = new ArrayList<>(); | ||
|
||
try (Connection conn = ConnectionManager.getConnection(); | ||
PreparedStatement pstmt = conn.prepareStatement(sql); | ||
ResultSet rs = pstmt.executeQuery();) { | ||
while (rs.next()) { | ||
T object = rowMapper.mapRow(rs); | ||
objects.add(object); | ||
} | ||
} | ||
return objects; | ||
} | ||
|
||
public T queryForObject(String sql, PreparedStatementSetter pstmtSetter, RowMapper<T> rowMapper) throws SQLException { | ||
ResultSet rs = null; | ||
T object = null; | ||
|
||
try (Connection conn = ConnectionManager.getConnection(); | ||
PreparedStatement pstmt = conn.prepareStatement(sql);){ | ||
pstmtSetter.setParameters(pstmt); | ||
rs = pstmt.executeQuery(); | ||
if (rs.next()) { | ||
object = rowMapper.mapRow(rs); | ||
} | ||
} finally { | ||
if (rs != null) | ||
rs.close(); | ||
} | ||
return object; | ||
} | ||
} |
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,11 @@ | ||
package core.jdbc; | ||
|
||
public class KeyHolder { | ||
private int id; | ||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
public int getId() { | ||
return id; | ||
} | ||
} |
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,15 @@ | ||
package core.jdbc; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
// 함수형 인터페이스 -> 추상 method 1개만 가짐 | ||
@FunctionalInterface | ||
public interface PreparedStatementSetter { | ||
|
||
// PreparedStatement의 parameter를 직접 설정해주는 역할 | ||
void setParameters(PreparedStatement ps) throws SQLException; | ||
} | ||
|
||
|
||
|
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,9 @@ | ||
package core.jdbc; | ||
import jwp.model.User; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
@FunctionalInterface | ||
public interface RowMapper<T> { | ||
T mapRow(ResultSet rs) throws SQLException; | ||
} |
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
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,35 @@ | ||
package jwp.controller; | ||
|
||
import core.mvc.Controller; | ||
import jwp.dao.QuestionDao; | ||
import jwp.model.Question; | ||
import jwp.model.User; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.sql.Date; | ||
import java.time.LocalDate; | ||
|
||
public class CreateQnaController implements Controller { | ||
|
||
private final QuestionDao questionDao = new QuestionDao(); | ||
|
||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { | ||
|
||
User user = (User) req.getSession().getAttribute("user"); | ||
if (user == null) { | ||
return "redirect:/user/login"; | ||
} | ||
|
||
String writer = user.getUserId(); | ||
|
||
String title = req.getParameter("title"); | ||
String contents = req.getParameter("contents"); | ||
|
||
Question question = new Question(0, writer, title, contents, Date.valueOf(LocalDate.now()), 0); | ||
questionDao.insert(question); | ||
|
||
return "redirect:/"; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,14 +1,27 @@ | ||
package jwp.controller; | ||
|
||
import core.mvc.Controller; | ||
import jwp.dao.QuestionDao; | ||
import jwp.model.Question; | ||
|
||
import javax.servlet.RequestDispatcher; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.List; | ||
|
||
public class HomeController implements Controller { | ||
|
||
private final QuestionDao questionDao = new QuestionDao(); | ||
|
||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { | ||
return "/home.jsp"; | ||
|
||
List<Question> questions = questionDao.findAll(); | ||
req.setAttribute("questions", questions); | ||
|
||
RequestDispatcher rd = req.getRequestDispatcher("/home.jsp"); | ||
rd.forward(req, resp); | ||
|
||
return null; | ||
Comment on lines
-12
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 forward를 안해도 됩니다...! /home.jsp를 리턴하면 DispatcherServlet이 forward를 해줘요 |
||
} | ||
} |
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
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
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,24 @@ | ||
package jwp.controller; | ||
|
||
import core.mvc.Controller; | ||
import jwp.model.User; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
public class QnaFormController implements Controller { | ||
|
||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { | ||
|
||
User user = (User) req.getSession().getAttribute("user"); | ||
if (user == null) { | ||
return "redirect:/user/login"; | ||
} | ||
|
||
String userId = user.getUserId(); | ||
req.setAttribute("userId", userId); | ||
|
||
return "/qna/form.jsp"; | ||
} | ||
} |
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,31 @@ | ||
package jwp.controller; | ||
|
||
import core.mvc.Controller; | ||
import jwp.dao.QuestionDao; | ||
import jwp.model.Question; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
public class ShowController implements Controller { | ||
|
||
private final QuestionDao questionDao = new QuestionDao(); | ||
|
||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { | ||
String questionIdParam = req.getParameter("questionId"); | ||
if (questionIdParam == null || questionIdParam.isEmpty()) { | ||
return "redirect:/"; | ||
} | ||
|
||
int questionId = Integer.parseInt(questionIdParam); | ||
Question question = questionDao.findByQuestionId(questionId); | ||
|
||
if (question == null) { | ||
return "redirect:/"; | ||
} | ||
|
||
req.setAttribute("question", question); | ||
return "/qna/show.jsp"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금은 필드주입으로 충분하지만 장기적인 관점에서 보면 생성자 주입을 사용하는 것이 더 좋을 것 같네요