MCPcopy Create free account
hub / github.com/castello/spring_basic / UserDao

Class UserDao

ch3/UserDao.java:12–159  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Implementers 1

UserDaoImplch3/UserDaoImpl.java

Calls

no outgoing calls

Tested by

no test coverage detected