| 13 | import java.util.Date; |
| 14 | |
| 15 | @Repository |
| 16 | public class UserDaoImpl implements UserDao { |
| 17 | @Autowired |
| 18 | DataSource ds; |
| 19 | |
| 20 | @Override |
| 21 | public int deleteUser(String id) throws Exception { |
| 22 | int rowCnt = 0; |
| 23 | String sql = "DELETE FROM user_info WHERE id= ? "; |
| 24 | |
| 25 | try ( // try-with-resources - since jdk7 |
| 26 | Connection conn = ds.getConnection(); |
| 27 | PreparedStatement pstmt = conn.prepareStatement(sql); |
| 28 | ){ |
| 29 | pstmt.setString(1, id); |
| 30 | return pstmt.executeUpdate(); // insert, delete, update |
| 31 | // } catch (Exception e) { |
| 32 | // e.printStackTrace(); |
| 33 | // throw e; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public User selectUser(String id) throws Exception { |
| 39 | User user = null; |
| 40 | String sql = "SELECT * FROM user_info WHERE id= ? "; |
| 41 | |
| 42 | try ( |
| 43 | Connection conn = ds.getConnection(); |
| 44 | PreparedStatement pstmt = conn.prepareStatement(sql); |
| 45 | ResultSet rs = pstmt.executeQuery(); // select |
| 46 | ){ |
| 47 | pstmt.setString(1, id); |
| 48 | |
| 49 | if (rs.next()) { |
| 50 | user = new User(); |
| 51 | user.setId(rs.getString(1)); |
| 52 | user.setPwd(rs.getString(2)); |
| 53 | user.setName(rs.getString(3)); |
| 54 | user.setEmail(rs.getString(4)); |
| 55 | user.setBirth(new Date(rs.getDate(5).getTime())); |
| 56 | user.setSns(rs.getString(6)); |
| 57 | user.setReg_date(new Date(rs.getTimestamp(7).getTime())); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return user; |
| 62 | } |
| 63 | |
| 64 | // 사용자 정보를 user_info테이블에 저장하는 메서드 |
| 65 | @Override |
| 66 | public int insertUser(User user) throws Exception { |
| 67 | int rowCnt = 0; |
| 68 | String sql = "INSERT INTO user_info VALUES (?,?,?,?,?,?, now()) "; |
| 69 | |
| 70 | try( |
| 71 | Connection conn = ds.getConnection(); |
| 72 | PreparedStatement pstmt = conn.prepareStatement(sql); // SQL Injection공격, 성능향상 |
nothing calls this directly
no outgoing calls
no test coverage detected