-
Notifications
You must be signed in to change notification settings - Fork 300
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
[JDBC 라이브러리 구현하기 - 2단계] 디노(신종화) 미션 제출합니다. #350
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
79347b4
refactor: `@FunctionalInterface` 어노테이션 추가
jjongwa d86a650
refactor: 중복 메서드 제거 및 ResultSet 리소스 반납
jjongwa 8e850d8
refactor: final 추가
jjongwa d02986b
refactor: exception 변경
jjongwa cf28768
refactor: 줄바꿈 동일하게 변경
jjongwa c62edf6
refactor: 리뷰 반영
jjongwa d3858bd
refactor: 템플릿 콜백 패턴 적용
jjongwa 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 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ | |||||||
import javax.sql.DataSource; | ||||||||
import org.slf4j.Logger; | ||||||||
import org.slf4j.LoggerFactory; | ||||||||
import org.springframework.dao.DataAccessException; | ||||||||
|
||||||||
public class JdbcTemplate { | ||||||||
|
||||||||
|
@@ -23,54 +24,54 @@ public JdbcTemplate(final DataSource dataSource) { | |||||||
|
||||||||
public void execute(final String sql, final Object... objects) { | ||||||||
try (final Connection conn = dataSource.getConnection(); | ||||||||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||||||||
final PreparedStatement pstmt = preparedStatementAndSetValue(conn, sql, objects) | ||||||||
) { | ||||||||
log.debug("query : {}", sql); | ||||||||
setValues(pstmt, objects); | ||||||||
pstmt.executeUpdate(); | ||||||||
} catch (SQLException e) { | ||||||||
log.error(e.getMessage(), e); | ||||||||
throw new RuntimeException(e); | ||||||||
throw new DataAccessException(e.getMessage()); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
public <T> List<T> query(final String sql, final RowMapper<T> rowMapper, final Object... objects) { | ||||||||
try (final Connection conn = dataSource.getConnection(); | ||||||||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||||||||
final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery() | ||||||||
) { | ||||||||
log.debug("query : {}", sql); | ||||||||
setValues(pstmt, objects); | ||||||||
final ResultSet rs = pstmt.executeQuery(); | ||||||||
final List<T> list = new ArrayList<>(); | ||||||||
while (rs.next()) { | ||||||||
list.add(rowMapper.mapRow(rs)); | ||||||||
} | ||||||||
return list; | ||||||||
} catch (SQLException e) { | ||||||||
log.error(e.getMessage(), e); | ||||||||
throw new RuntimeException(e); | ||||||||
throw new DataAccessException(e.getMessage()); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
@Nullable | ||||||||
public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... objects) { | ||||||||
try (final Connection conn = dataSource.getConnection(); | ||||||||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||||||||
final ResultSet rs = preparedStatementAndSetValue(conn, sql, objects).executeQuery() | ||||||||
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.
Suggested change
조그마한 제안입니다 ㅎㅎ 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. 아주 좋습니다ㅎㅎ |
||||||||
) { | ||||||||
log.debug("query : {}", sql); | ||||||||
setValues(pstmt, objects); | ||||||||
final ResultSet rs = pstmt.executeQuery(); | ||||||||
if (rs.next()) { | ||||||||
return rowMapper.mapRow(rs); | ||||||||
} | ||||||||
return null; | ||||||||
} catch (SQLException e) { | ||||||||
log.error(e.getMessage(), e); | ||||||||
throw new RuntimeException(e); | ||||||||
throw new DataAccessException(e.getMessage()); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
private void setValues(final PreparedStatement pstmt, Object... objects) throws SQLException { | ||||||||
private PreparedStatement preparedStatementAndSetValue( | ||||||||
final Connection conn, | ||||||||
final String sql, | ||||||||
final Object... objects | ||||||||
) throws SQLException { | ||||||||
final PreparedStatement pstmt = conn.prepareStatement(sql); | ||||||||
log.debug("query : {}", sql); | ||||||||
setValues(pstmt, objects); | ||||||||
return pstmt; | ||||||||
} | ||||||||
|
||||||||
private void setValues(final PreparedStatement pstmt, final Object... objects) throws SQLException { | ||||||||
for (int i = 0; i < objects.length; i++) { | ||||||||
pstmt.setObject(i + 1, objects[i]); | ||||||||
} | ||||||||
|
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
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.
지금 당장 크게 중요한 부분은 아니지만..!!
예외를 전환할 때에는 기존예외를 포함해주는게 좋다고 합니다!!
기존 예외를 포함해야 추후에 에러에 대한 로그를 찍을 때 기존 예외의 스택 트레이스를 확인할 수 있기 때문입니당 (Caused by 부분)
현재 뼈대코드에서 제공되는
DataAccessException
에는Throwable cause
를 파라미터로 받는 생성자도 열려있어서 얘를 사용하면 좋을 것 같아요 ㅎㅎ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.
Caused by에 저런 식으로 나올거라고 생각하지 못했네요.. e를 담아서 던지도록 변경했습니다.!
c62edf6