Skip to content

Commit

Permalink
refactor: final 키워드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kong-hana01 committed Sep 29, 2023
1 parent 210054f commit 46d3655
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# JDBC 라이브러리 구현하기

## 1. JDBC 라이브러리 구현하기
- [ ] SQL 쿼리 작성, 쿼리에 전달할 인자, 조회 결과를 추출하는 것에만 집중할 수 있도록 라이브러리 만들기
- [ ] 중복제거를 위해 JdbcTemplate을 구현한다.
- [ ] 기존 Dao에서 활용하던 DataSource 대신 JdbcTemplate을 사용하도록 변경한다.
- [x] SQL 쿼리 작성, 쿼리에 전달할 인자, 조회 결과를 추출하는 것에만 집중할 수 있도록 라이브러리 만들기
- [x] 중복제거를 위해 JdbcTemplate을 구현한다.
- [x] 기존 Dao에서 활용하던 DataSource 대신 JdbcTemplate을 사용하도록 변경한다.
23 changes: 11 additions & 12 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public JdbcTemplate(final DataSource dataSource) {
public void execute(final String sql, final PreparedStatementSetter preparedStatementSetter) {
log.debug("query : {}", sql);

try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement ps = conn.prepareStatement(sql)) {

preparedStatementSetter.set(ps);
ps.executeUpdate();
Expand All @@ -40,8 +40,8 @@ public <T> T find(final String sql,
final ResultSetGetter<T> rsg) {
log.debug("query : {}", sql);

try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement ps = conn.prepareStatement(sql)) {

preparedStatementSetter.set(ps);
return getObject(rsg, ps);
Expand All @@ -52,20 +52,19 @@ public <T> T find(final String sql,
}

private <T> T getObject(ResultSetGetter<T> rsg, PreparedStatement ps) throws SQLException {
try (ResultSet rs = ps.executeQuery()) {
try (final ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
return rsg.getObject(rs);
}
return null;
}
}

public <T> List<T> findAll(final String sql,
final ResultSetGetter<T> rsg) {
public <T> List<T> findAll(final String sql, final ResultSetGetter<T> rsg) {
log.debug("query : {}", sql);

try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement ps = conn.prepareStatement(sql)) {

return getObjects(rsg, ps);
} catch (SQLException e) {
Expand All @@ -74,9 +73,9 @@ public <T> List<T> findAll(final String sql,
}
}

private <T> List<T> getObjects(ResultSetGetter<T> rsg, PreparedStatement ps) throws SQLException {
try (ResultSet rs = ps.executeQuery()) {
List<T> objects = new ArrayList<>();
private <T> List<T> getObjects(final ResultSetGetter<T> rsg, final PreparedStatement ps) throws SQLException {
try (final ResultSet rs = ps.executeQuery()) {
final List<T> objects = new ArrayList<>();
while (rs.next()) {
objects.add(rsg.getObject(rs));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PreparedStatementSetter(final List<Object> values) {

public void set(final PreparedStatement ps) throws SQLException {
for (int i = 0; i < values.size(); i++) {
PreparedStatementTypeValue pstv = values.get(i);
final PreparedStatementTypeValue pstv = values.get(i);
pstv.setPreparedStatement(ps, i+1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ResultSetGetter(final Map<String, Class<?>> values, final Class<T> classF

public T getObject(final ResultSet rs) throws SQLException {
try {
T instance = classForObject.getConstructor().newInstance();
final T instance = classForObject.getConstructor().newInstance();
setField(rs, instance);
return instance;
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException |
Expand All @@ -31,10 +31,10 @@ public T getObject(final ResultSet rs) throws SQLException {
}
}

private void setField(ResultSet rs, T instance) throws SQLException, NoSuchFieldException, IllegalAccessException {
for (Entry<String, SqlType> entry : values.entrySet()) {
Object value = getValue(rs, entry.getValue(), entry.getKey());
Field field = instance.getClass().getDeclaredField(entry.getKey());
private void setField(final ResultSet rs, final T instance) throws SQLException, NoSuchFieldException, IllegalAccessException {
for (final Entry<String, SqlType> entry : values.entrySet()) {
final Object value = getValue(rs, entry.getValue(), entry.getKey());
final Field field = instance.getClass().getDeclaredField(entry.getKey());
field.setAccessible(true);
field.set(instance, value);
}
Expand Down

0 comments on commit 46d3655

Please sign in to comment.