| 10 | import java.util.Date; |
| 11 | |
| 12 | public class UserDaoImpl implements UserDao { |
| 13 | @Autowired |
| 14 | DataSource ds; |
| 15 | final int FAIL = 0; |
| 16 | |
| 17 | @Override |
| 18 | public int deleteUser(String id) { |
| 19 | int rowCnt = FAIL; // insert, delete, update |
| 20 | |
| 21 | Connection conn = null; |
| 22 | PreparedStatement pstmt = null; |
| 23 | |
| 24 | String sql = "delete from user_info where id= ? "; |
| 25 | |
| 26 | try { |
| 27 | conn = ds.getConnection(); |
| 28 | pstmt = conn.prepareStatement(sql); |
| 29 | pstmt.setString(1, id); |
| 30 | // int rowCnt = pstmt.executeUpdate(); // insert, delete, update |
| 31 | // return rowCnt; |
| 32 | return pstmt.executeUpdate(); // insert, delete, update |
| 33 | } catch (SQLException e) { |
| 34 | e.printStackTrace(); |
| 35 | return FAIL; |
| 36 | } finally { |
| 37 | // close()를 호출하다가 예외가 발생할 수 있으므로, try-catch로 감싸야함. |
| 38 | // try { if(pstmt!=null) pstmt.close(); } catch (SQLException e) { e.printStackTrace();} |
| 39 | // try { if(conn!=null) conn.close(); } catch (SQLException e) { e.printStackTrace();} |
| 40 | close(pstmt, conn); // private void close(AutoCloseable... acs) { |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public User selectUser(String id) throws Exception { |
| 46 | User user = null; |
| 47 | |
| 48 | Connection conn = null; |
| 49 | PreparedStatement pstmt = null; |
| 50 | ResultSet rs = null; |
| 51 | |
| 52 | String sql = "select * from user_info where id= ? "; |
| 53 | |
| 54 | try { |
| 55 | conn = ds.getConnection(); |
| 56 | pstmt = conn.prepareStatement(sql); // SQL Injection공격, 성능향상 |
| 57 | pstmt.setString(1, id); |
| 58 | |
| 59 | rs = pstmt.executeQuery(); // select |
| 60 | |
| 61 | if (rs.next()) { |
| 62 | user = new User(); |
| 63 | user.setId(rs.getString(1)); |
| 64 | user.setPwd(rs.getString(2)); |
| 65 | user.setName(rs.getString(3)); |
| 66 | user.setEmail(rs.getString(4)); |
| 67 | user.setBirth(new Date(rs.getDate(5).getTime())); |
| 68 | user.setSns(rs.getString(6)); |
| 69 | user.setReg_date(new Date(rs.getTimestamp(7).getTime())); |
nothing calls this directly
no outgoing calls
no test coverage detected