Jsp - jdbc를 활용한 회원정보활용

2020. 6. 27. 21:27Jsp

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
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ 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>jsp3/jdbctest.jsp</h1>
<%
//jdbc(Java Database Connectivity) 프로그램 설치
Class.forName("com.mysql.jdbc.Driver");
String dburl="jdbc:mysql://localhost:3306/jspdb5";
String dbUser="root";
String dbPass="1234";
//
Connection con= DriverManager.getConnection(dburl, dbUser, dbPass);
%>
<%="연결성공" %>
</body>
</html>
 
<%@ 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>jsp3/insertform.jsp</h1>
<form action="insertpro.jsp" method="post">
학생번호 : <input type="text" name="num"><br>
학생이름 : <input type="text" name="name"><br>
<input type="submit" value="학생등록">
 
 
</form>
</body>
</html>
 
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>jsp3/insertpro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
//request 한글처리
//request   int num     String  name  가져오기
int num=Integer.parseInt(request.getParameter("num"));
String name=request.getParameter("name");
 
out.println(num);
out.println(name);
//1단계 드라이버 불러오기
Class.forName("com.mysql.jdbc.Driver");
 
//2단계 디비연결-> 연결정보 저장
String dburl="jdbc:mysql://localhost:3306/jspdb5";
String dbUser="root";
String dbPass="1234";
Connection con= DriverManager.getConnection(dburl, dbUser, dbPass);
//3단계 연결정보를 이용해서 sql구문 만들고 실행할 객체생성
// String sql="insert into student(num,name) value("+num+", '"+name+"')";
String sql="insert into student(num,name) value(?,?)";//더 간편함.
PreparedStatement pstmt=con.prepareStatement(sql);
//?값 넣기       set형 (물음표 순서, 값)
        pstmt.setInt(1,num);
pstmt.setString(2,name);
 
 
//4단계 객체 (sql구문) 실행                 insert, delete, update 만 사용가능 select는 query
pstmt.executeUpdate();
%>
학생등록성공<%=pstmt %>
</body>
</html>
 
 
<%@ 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>jsp3/selectForm.jsp</h1>
<form action="selectPro.jsp" method="post">
조회할 학생번호 : <input type="text" name="num"><br>
<input type="submit" value="학생조회">
</form>
</body>
</html>
 
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>jsp3/selectPro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
 
int num=Integer.parseInt(request.getParameter("num"));
 
Class.forName("com.mysql.jdbc.Driver");
String dburl="jdbc:mysql://localhost:3306/jspdb5";
String dbUser="root";
String dbPass="1234";
 
Connection con= DriverManager.getConnection(dburl, dbUser, dbPass);
 
String sql="select * from student where num=?";
PreparedStatement pstmt=con.prepareStatement(sql);
 
pstmt.setInt(1,num);
ResultSet rs=pstmt.executeQuery();
%>
학생조회성공<%=pstmt %><br>
<table border="1">
<tr><td>학생번호</td><td>학생이름</td></tr>
<%
while(rs.next()){    
    %>
    <tr><td><%=rs.getInt("num"%></td><td><%=rs.getString("name"%></td></tr>
    <%
}
 
%>
 
</table>
</body>
</html>
 
 
<%@ 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>jsp3/updateForm.jsp</h1>
<form action="updatePro.jsp" method="post">
학생번호 : <input type="text" name="num"><br>
수정할 학생이름 : <input type="text" name="name"><br>
<input type="submit" value="학생수정">
</form>
</body>
</html>
 
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>jsp3/updatePro.jsp</h1>
<%
//request 한글처리
request.setCharacterEncoding("UTF-8");
 
//int num,  String name <=request 파라미터 가져와서 저장
int num=Integer.parseInt(request.getParameter("num"));
String name=request.getParameter("name");
//1단계 드라이버 불러오기
Class.forName("com.mysql.jdbc.Driver");
 
//2단계 드라이버를 이용해서 디비연결-> Connection con 연결정보 저장
String dburl="jdbc:mysql://localhost:3306/jspdb5";
String dbUser="root";
String dbPass="1234";
Connection con=DriverManager.getConnection(dburl, dbUser, dbPass);
//sql update 테이블 이름 set 이름수정 where 조건
//3단계 con 이용해서 연결정보에 prepareStatement(sql) 메서드 호출
//                                sql 만들고 실행할 수 있는 PreparedStatement pstmt 생성
String sql="update student set name=? where num=?";
PreparedStatement pstmt=con.prepareStatement(sql);
 
pstmt.setString(1,name);
 
pstmt.setInt(2,num);
//4단계 실행
pstmt.executeUpdate();
%>
학생수정성공<%=pstmt %>
</body>
</html>
 
 
<%@ 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>jsp3/deleteForm.jsp</h1>
<form action="deletePro.jsp" method="post">
삭제할 학생번호 : <input type="text" name="num"><br>
삭제할 학생이름 : <input type="text" name="name"><br>
<input type="submit" value="학생삭제">
 
 
</form>
</body>
</html>
 
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>jsp3/deletePro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
 
int num=Integer.parseInt(request.getParameter("num"));
String name=request.getParameter("name");
 
Class.forName("com.mysql.jdbc.Driver");
String dburl="jdbc:mysql://localhost:3306/jspdb5";
String dbUser="root";
String dbPass="1234";
 
Connection con=DriverManager.getConnection(dburl, dbUser, dbPass);
 
String sql="delete from student where num=? and name=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, num);
pstmt.setString(2,name);
pstmt.executeUpdate();
 
 
%>
학생 삭제 완료<%=pstmt %>
</body>
</html>
cs

 

jdbc라는 eclipse를 활용한 자바를 기반으로 데이터베이스와 연결해주는 연결 프로그램을 배워보았다. 

jdbc를 활용하여 eclipse에서 Driver 를 연결해 데이터베이스와  연결해서 웹페이지에서 입력한 정보들을

데이터베이스로 생성시키고 삭제,조회,수정 을 하는 작업을 해보았는데 하나의 작업을 위해

두 개의 페이지가 필요했다. 하나는 화면에 보이기 위한 페이지와 그것과 연결된 뒤에서 작업하기 위한 페이지를 만들어서 사용자 입장에서는 보이는 페이지에서 클릭만 해도 여러 작업들이 이루어지게 만들어 주는 것을 배웠다.

위에 올린 코드들을 보면 알겠지만 jdbc를 연결하는 코드들이 계속 반복적으로 사용되어지는데 이것들을 줄일 필요가 있어 보였다. 좀 더 있다가 배운다고 하였는데 얼른 배우고 싶다!

'Jsp' 카테고리의 다른 글

Jsp - file upload  (0) 2020.07.26
Jsp - paging  (0) 2020.07.26
Jsp - session!!!  (0) 2020.06.27
Jsp - cookie  (0) 2020.06.27
Jsp - session  (0) 2020.06.27