forked from castello/spring_basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBConnectionTest2Test.java
More file actions
144 lines (110 loc) · 4.96 KB
/
Copy pathDBConnectionTest2Test.java
File metadata and controls
144 lines (110 loc) · 4.96 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.fastcampus.ch3;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.sql.DataSource;
import javax.swing.tree.ExpandVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"})
public class DBConnectionTest2Test {
@Autowired
DataSource ds;
@Test
public void insertUserTest() throws Exception {
User user = new User("asdf2", "1234", "abc", "aaaa@aaa.com", new Date(), "fb", new Date());
deleteAll();
int rowCnt = insertUser(user);
assertTrue(rowCnt==1);
}
@Test
public void selectUserTest() throws Exception {
deleteAll();
User user = new User("asdf2", "1234", "abc", "aaaa@aaa.com", new Date(), "fb", new Date());
int rowCnt = insertUser(user);
User user2 = selectUser("asdf2");
assertTrue(user.getId().equals("asdf2"));
}
@Test
public void deleteUserTest() throws Exception {
deleteAll();
int rowCnt = deleteUser("asdf");
assertTrue(rowCnt==0);
User user = new User("asdf2", "1234", "abc", "aaaa@aaa.com", new Date(), "fb", new Date());
rowCnt = insertUser(user);
assertTrue(rowCnt==1);
rowCnt = deleteUser(user.getId());
assertTrue(rowCnt==1);
assertTrue(selectUser(user.getId())==null);
}
// 매개변수로 받은 사용자 정보로 user_info테이블을 update하는 메서드
public int updateUser(User user) throws Exception {
return 0;
}
public int deleteUser(String id) throws Exception {
Connection conn = ds.getConnection();
String sql = "delete from user_info where id= ? ";
PreparedStatement pstmt = conn.prepareStatement(sql); // SQL Injection공격, 성능향상
pstmt.setString(1, id);
// int rowCnt = pstmt.executeUpdate(); // insert, delete, update
// return rowCnt;
return pstmt.executeUpdate(); // insert, delete, update
}
public User selectUser(String id) throws Exception {
Connection conn = ds.getConnection();
String sql = "select * from user_info where id= ? ";
PreparedStatement pstmt = conn.prepareStatement(sql); // SQL Injection공격, 성능향상
pstmt.setString(1,id);
ResultSet rs = pstmt.executeQuery(); // select
if(rs.next()) {
User user = new User();
user.setId(rs.getString(1));
user.setPwd(rs.getString(2));
user.setName(rs.getString(3));
user.setEmail(rs.getString(4));
user.setBirth(new Date(rs.getDate(5).getTime()));
user.setSns(rs.getString(6));
user.setReg_date(new Date(rs.getTimestamp(7).getTime()));
return user;
}
return null;
}
private void deleteAll() throws Exception {
Connection conn = ds.getConnection();
String sql = "delete from user_info ";
PreparedStatement pstmt = conn.prepareStatement(sql); // SQL Injection공격, 성능향상
pstmt.executeUpdate(); // insert, delete, update
}
// 사용자 정보를 user_info테이블에 저장하는 메서드
public int insertUser(User user) throws Exception {
Connection conn = ds.getConnection();
// insert into user_info (id, pwd, name, email, birth, sns, reg_date)
// values ('asdf22', '1234', 'smith', 'aaa@aaa.com', '2022-01-01', 'facebook', now());
String sql = "insert into user_info values (?, ?, ?, ?,?,?, now()) ";
PreparedStatement pstmt = conn.prepareStatement(sql); // SQL Injection공격, 성능향상
pstmt.setString(1, user.getId());
pstmt.setString(2, user.getPwd());
pstmt.setString(3, user.getName());
pstmt.setString(4, user.getEmail());
pstmt.setDate(5, new java.sql.Date(user.getBirth().getTime()));
pstmt.setString(6, user.getSns());
int rowCnt = pstmt.executeUpdate(); // insert, delete, update
return rowCnt;
}
@Test
public void springJdbcConnectionTest() throws Exception {
// ApplicationContext ac = new GenericXmlApplicationContext("file:src/main/webapp/WEB-INF/spring/**/root-context.xml");
// DataSource ds = ac.getBean(DataSource.class);
Connection conn = ds.getConnection(); // 데이터베이스의 연결을 얻는다.
System.out.println("conn = " + conn);
assertTrue(conn!=null); // 괄호 안의 조건식이 true면, 테스트 성공, 아니면 실패
}
}