-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTotalCountRepositoryImpl.java
More file actions
80 lines (67 loc) · 2.42 KB
/
TotalCountRepositoryImpl.java
File metadata and controls
80 lines (67 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.web.mzvoca.repository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.sql.*;
import java.util.NoSuchElementException;
@Repository
@RequiredArgsConstructor
@Slf4j
public class TotalCountRepositoryImpl implements TotalCountRepository {
private final DataSource dataSource;
@Override
public int totalCountRead() {
String sql = "select * from totalcount";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
// 가져온 데이터가 있을 때 실행할 부분
int totalCount = rs.getInt("total_count");
return totalCount;
} else {
throw new NoSuchElementException("값이 없습니다.");
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(con, pstmt, rs);
}
}
@Override
public void totalCountUpdate() {
// Update문을 통해 totalCount 값을 1 증가
String sql = "update totalcount set total_count = total_count + 1";
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
pstmt = con.prepareStatement(sql);
int result = pstmt.executeUpdate();
log.info("실행된 행 개수={}", result);
} catch (SQLException e) {
log.info("db Error", e);
throw new RuntimeException(e);
} finally {
close(con, pstmt, null);
}
}
private Connection getConnection() throws SQLException {
// 트랜잭션 동기화를 위해 DataSourceUtils 메서드 사용
Connection con = DataSourceUtils.getConnection(dataSource);
return con;
}
private void close(Connection con, Statement stmt, ResultSet rs) {
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
// 트랜잭션 동기화를 위해 DataSourceUtils 메서드 사용
DataSourceUtils.releaseConnection(con, dataSource);
}
}