MVC Model 1 :: MV 구조
CustUserDao
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import dto.CustUserDto;
// model
public class CustUserDao {
private static CustUserDao dao = new CustUserDao();
private CustUserDao() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("OracleDriver load Success");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static CustUserDao getInstance() {
return dao;
}
public List<CustUserDto> getCustUserList() {
String sql = " SELECT ID, NAME, ADDRESS "
+ " FROM CUSTUSER "
+ " ORDER BY ID DESC ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
List<CustUserDto> list = new ArrayList<CustUserDto>();
try {
conn = this.getConnection();
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while(rs.next()) {
String id = rs.getString("ID");
String name = rs.getString("NAME");
String address = rs.getString("ADDRESS");
list.add( new CustUserDto(id, name, address) );
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(conn, psmt, rs);
}
return list;
}
public boolean addCustUser(CustUserDto dto) {
String sql = " INSERT INTO CUSTUSER(ID, NAME, ADDRESS) "
+ " VALUES(?, ?, ?) ";
Connection conn = null;
PreparedStatement psmt = null;
int count = 0;
try {
conn = this.getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getId());
psmt.setString(2, dto.getName());
psmt.setString(3, dto.getAddress());
count = psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close(conn, psmt, null);
}
return count>0?true:false;
}
public CustUserDto getCustuser(String id) {
String sql = " SELECT ID, NAME, ADDRESS "
+ " FROM CUSTUSER"
+ " WHERE ID=? ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
CustUserDto dto = null;
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, id.trim());
rs = psmt.executeQuery();
if(rs.next()) {
dto = new CustUserDto();
dto.setId( rs.getString("ID") );
dto.setName( rs.getString("NAME") );
dto.setAddress( rs.getString("ADDRESS") );
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, psmt, rs);
}
return dto;
}
public boolean deleteCustUser(String id) {
String sql = " DELETE "
+ " FROM CUSTUSER "
+ " WHERE ID=? ";
Connection conn = null;
PreparedStatement psmt = null;
int count = 0;
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, id.trim());
count = psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, psmt, null);
}
return count>0?true:false;
}
public boolean deleteCustUsers(String ids[]) {
String sql = " DELETE FROM CUSTUSER "
+ " WHERE ID=? ";
Connection conn = null;
PreparedStatement psmt = null;
int count[] = new int[ids.length];
try {
conn = getConnection();
conn.setAutoCommit(false); // 작업하는 동안 잠시 commit 보류
psmt = conn.prepareStatement(sql);
for(int i = 0;i < ids.length; i++) {
psmt.setString(1, ids[i]);
psmt.addBatch(); // 잠시 쌓아뒀다가
}
count = psmt.executeBatch(); // executeBatch로 한꺼번에 처리
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {}
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
close(conn, psmt, null);
}
boolean isS = true;
for (int i = 0; i < count.length; i++) {
if(count[i] != -2) { // -2 : 정상종료
isS = false;
break;
}
}
return isS;
}
public int updateCustUser(String id, String name, String address) {
String sql = " UPDATE CUSTUSER "
+ " SET NAME=?, ADDRESS=?"
+ " WHERE ID=? ";
Connection conn = null;
PreparedStatement psmt = null;
int count = 0;
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, name.trim());
psmt.setString(2, address.trim());
psmt.setString(3, id.trim());
count = psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
close(conn, psmt, null);
}
return count;
}
public Connection getConnection()throws SQLException {
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "hr";
String password = "hr";
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public void close(Connection conn, PreparedStatement psmt, ResultSet rs) {
try {
if(conn != null) {
conn.close();
}
if(psmt != null) {
psmt.close();
}
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
|
cs |
custuseradd
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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>고객 추가</h1>
<form action="custuseraddAf.jsp">
<table>
<col width="100px"><col width="300px">
<tr>
<td height="2" bgcolor="#ff0000" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
<th>아이디</th>
<td>
<input type="text" name="id" size="20">
</td>
</tr>
<tr>
<td height="2" bgcolor="#ff0000" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
<th>이름</th>
<td>
<input type="text" name="name" size="20">
</td>
</tr>
<tr>
<td height="2" bgcolor="#ff0000" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
<th>주소</th>
<td>
<input type="text" name="address" size="20">
</td>
</tr>
<tr>
<td height="2" bgcolor="#ff0000" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
<td align="center" colspan="2">
<input type="submit" value="고객추가">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</body>
</html>
|
cs |
custuseraddAf
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
|
<%@page import="dto.CustUserDto"%>
<%@page import="dao.CustUserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
System.out.println(id + " " + name + " " + address);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
CustUserDao dao = CustUserDao.getInstance();
boolean isS = dao.addCustUser(new CustUserDto(id, name, address));
if(isS){
%>
<script type="text/javascript">
alert('성공적으로 추가되었습니다');
location.href = "custuserlist.jsp";
</script>
<%
}else{
%>
<script type="text/javascript">
alert('추가되지 않았습니다');
location.href = "custuseradd.jsp";
</script>
<%
}
%>
</body>
</html>
|
cs |
custuserdelete
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
|
<%@page import="dao.CustUserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("id");
CustUserDao dao = CustUserDao.getInstance();
boolean isS = dao.deleteCustUser(id);
if(isS){
%>
<script type="text/javascript">
alert("정상적으로 삭제되었습니다");
location.href = "custuserlist.jsp";
</script>
<%
}else{
%>
<script type="text/javascript">
alert("삭제되지 않았습니다");
location.href = "custuserlist.jsp";
</script>
<%
}
%>
</body>
</html>
|
cs |
custuserdetail
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
|
<%@page import="dto.CustUserDto"%>
<%@page import="dao.CustUserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = request.getParameter("id");
System.out.println("id:" + id);
CustUserDao dao = CustUserDao.getInstance();
CustUserDto dto = dao.getCustuser(id);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>custuserdetail</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>고객정보</h1>
<table style="width: 600">
<col width="200"><col width="400">
<tr>
<td height="2" bgcolor="#00ff00" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
<th>ID</th>
<td id="id"><%=dto.getId() %></td>
</tr>
<tr>
<td height="2" bgcolor="#00ff00" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
<th>이름</th>
<td><%=dto.getName() %></td>
</tr>
<tr>
<td height="2" bgcolor="#00ff00" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
<th>주소</th>
<td><%=dto.getAddress() %></td>
</tr>
<tr>
<td height="2" bgcolor="#00ff00" colspan="3"></td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="button" id="updateBtn">수정</button>
<%--
<form action="custuserupdate.jsp">
<input type="hidden" name="id" value="<%=dto.getId() %>">
<input type="submit" value="수정">
</form>
--%>
<button type="button" id="deleteBtn">삭제</button>
</td>
</tr>
<tr>
<td height="2" bgcolor="#00ff00" colspan="3"></td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function () {
$("#updateBtn").click(function () {
// alert('updateBtn');
// location.href = "custuserupdate.jsp?id=" + $("#id").text();
// let id = $("#id").text();
// alert(id);
let id = "<%=dto.getId() %>";
alert("회원 수정 페이지로 이동합니다");
location.href= "custuserupdate.jsp?id=" + $("#id").text();
});
});
</script>
</body>
</html>
|
cs |
custuserlist.jsp
custuserupdate.jsp
custuserupdateAf.jsp
index.jsp
mulder.jsp
는 개인적으로 참고!!
'개발 > JAVA' 카테고리의 다른 글
[JAVA] #07. 논리 연산자 & 삼항 연산자 (0) | 2020.07.28 |
---|---|
[JSP] #03. MVC model 1과 model 2 같은 코드로 비교(CustUser) (2) (0) | 2020.07.23 |
[JAVA&JSP] #01. MVC 구조 :: MODEL 1 & MODEL 2 (0) | 2020.07.22 |
[Servlet] #07. ★html -> 서블릿, 서블릿->서블릿, 서블릿->JSP 링크 옮기기 (0) | 2020.07.21 |
[Servlet] #06. Cookie(쿠키) :: id, pwd, 방문횟수 (0) | 2020.07.21 |