-
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 라이브러리 구현하기 - 1단계] 제이미(임정수) 미션 제출합니다! #335
Changes from 6 commits
dc70d1d
7182a49
bfca4b0
b1184df
d931c04
5b2b024
72180f0
084732b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,12 @@ | |
import org.slf4j.LoggerFactory; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class JdbcTemplate { | ||
|
||
|
@@ -14,4 +20,76 @@ public class JdbcTemplate { | |
public JdbcTemplate(final DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public int update(final String sql, final Object... args) { | ||
try ( | ||
final Connection conn = dataSource.getConnection(); | ||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||
) { | ||
setArguments(pstmt, args); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
return pstmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static void setArguments(final PreparedStatement pstmt, final Object[] args) throws SQLException { | ||
for (int i = 0; i < args.length; i++) { | ||
final int parameterIndex = i + 1; | ||
pstmt.setObject(parameterIndex, args[i]); | ||
} | ||
} | ||
|
||
public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... args) { | ||
try ( | ||
final Connection conn = dataSource.getConnection(); | ||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||
) { | ||
setArguments(pstmt, args); | ||
ResultSet rs = pstmt.executeQuery(); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
return calculateResult(rowMapper, rs); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static <T> T calculateResult(final RowMapper<T> rowMapper, final ResultSet rs) throws SQLException { | ||
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.
|
||
if (rs.next()) { | ||
return rowMapper.mapRow(rs, rs.getRow()); | ||
} | ||
Comment on lines
+63
to
+65
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.
|
||
|
||
return null; | ||
} | ||
|
||
public <T> List<T> query(final String sql, final RowMapper<T> rowMapper) { | ||
try ( | ||
final Connection conn = dataSource.getConnection(); | ||
final PreparedStatement pstmt = conn.prepareStatement(sql) | ||
) { | ||
ResultSet rs = pstmt.executeQuery(); | ||
|
||
log.debug("query : {}", sql); | ||
|
||
return getResults(rowMapper, rs); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static <T> List<T> getResults(final RowMapper<T> rowMapper, final ResultSet rs) throws SQLException { | ||
List<T> results = new ArrayList<>(); | ||
|
||
while (rs.next()) { | ||
final T result = calculateResult(rowMapper, rs); | ||
results.add(result); | ||
} | ||
|
||
return results; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.springframework.jdbc.core; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
@FunctionalInterface | ||
public interface RowMapper<T> { | ||
|
||
T mapRow(final ResultSet rs, final int rowNum) throws SQLException; | ||
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. 제가 1단계를 진행하면서 느낀 궁금점인데요 ! RowMapper에서 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. 해당 파라미터는 그랬던 것 같아서 넣은 거였는데 제가 전혀 사용하지 않는군요..! |
||
} |
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.
컬럼의 index와 컬럼의 name으로 추출하는 두 가지의 방법이 존재하는데 인덱스로 추출해주신 이유가 궁금합니다 ㅎㅎ
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.
기존에 코드가 그렇게 주어졌기에 크게 생각하지 않고 바로 인덱스로 추출하는 방식을 사용하게 되었습니다..!
그레이가 말씀해 주셔서 고민해 본 결과 저는 컴럼명을 통해 추출하는 방식이 보다 명확해 보여서 좋을 것 같네요.
다만, 컬럼명이 수정된다면 계속 변경해야겠지만, 그럴 일은 적을 것 같아 가독성을 선택하도록 하겠습니다!
해당 방법으로 수정해 두었습니다.
그레이는 어떤 방식으로 진행하셨고 이유가 무엇인지 궁금하네요!
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.
저는 컬럼 순서보다 컬럼 명이 읽기가 명확하고, 잘못된 값을 가져오는 상황을 방지할 수 있기 때문에 컬럼 명으로 가져오는 방법을 선택했씁니다 ㅎㅎ