質問編集履歴

1

サーブレットの内容

2020/09/22 01:58

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,145 +4,319 @@
4
4
 
5
5
  ■Servlet
6
6
 
7
- package ryoukin.java;
8
-
9
-
10
-
11
-
12
-
13
- public class ryoukin_beans {
14
-
15
-
16
-
17
- private int coursea_value;
18
-
19
- private int courseb_value;
20
-
21
- private int coursec_value;
22
-
23
- private int opcoursea_value;
24
-
25
- private int opcourseb_value;
26
-
27
- private int opcoursec_value;
28
-
29
- public ryoukin_beans(int coursea_value, int courseb_value, int coursec_value, int opcoursea_value,
30
-
31
- int opcourseb_value, int opcoursec_value) {
32
-
33
- super();
34
-
35
- this.coursea_value = coursea_value;
36
-
37
- this.courseb_value = courseb_value;
38
-
39
- this.coursec_value = coursec_value;
40
-
41
- this.opcoursea_value = opcoursea_value;
42
-
43
- this.opcourseb_value = opcourseb_value;
44
-
45
- this.opcoursec_value = opcoursec_value;
7
+ package seisan;
8
+
9
+
10
+
11
+ import java.io.IOException;
12
+
13
+ import java.sql.Connection;
14
+
15
+ import java.sql.DriverManager;
16
+
17
+ import java.sql.PreparedStatement;
18
+
19
+ import java.sql.ResultSet;
20
+
21
+ import java.sql.SQLException;
22
+
23
+
24
+
25
+ import javax.annotation.Resource;
26
+
27
+ import javax.servlet.ServletException;
28
+
29
+ import javax.servlet.annotation.WebServlet;
30
+
31
+ import javax.servlet.http.HttpServlet;
32
+
33
+ import javax.servlet.http.HttpServletRequest;
34
+
35
+ import javax.servlet.http.HttpServletResponse;
36
+
37
+ import javax.sql.DataSource;
38
+
39
+
40
+
41
+ import ryoukin.java.ryoukin_beans;
42
+
43
+
44
+
45
+ /**
46
+
47
+ * Servlet implementation class SeisanServlet
48
+
49
+ */
50
+
51
+ @WebServlet("/Seisan")
52
+
53
+ public class SeisanServlet extends HttpServlet {
54
+
55
+ private static final long serialVersionUID = 1L;
56
+
57
+
58
+
59
+ // Define datasource/connection pool for Resource Injection
60
+
61
+ @Resource(name="jdbc/WebApp")
62
+
63
+ private DataSource dataSource;
64
+
65
+
66
+
67
+ /**
68
+
69
+ * @see HttpServlet#HttpServlet()
70
+
71
+ */
72
+
73
+ public SeisanServlet() {
74
+
75
+ super();
76
+
77
+ // TODO Auto-generated constructor stub
78
+
79
+ }
80
+
81
+
82
+
83
+ /**
84
+
85
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
86
+
87
+ */
88
+
89
+ @SuppressWarnings("unused")
90
+
91
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
92
+
93
+ // TODO Auto-generated method stub
94
+
95
+ response.getWriter().append("Served at: ").append(request.getContextPath());
96
+
97
+ //文字化け対策
98
+
99
+ response.setContentType("text/html; charset=UTF-8");
100
+
101
+ response.setContentType("text/html; charset=UTF-8");
102
+
103
+ // 【料金情報を取得する】
104
+
105
+ ryoukin_beans ryoukin = null;
106
+
107
+
108
+
109
+ // ■DB接続
110
+
111
+ Connection conn = null;
112
+
113
+ PreparedStatement ps = null;
114
+
115
+ ResultSet rs = null;
116
+
117
+
118
+
119
+ // 接続情報
120
+
121
+ String url="jdbc:mariadb://localhost:3306/webApp";
122
+
123
+ String user="user02";
124
+
125
+ String password="user02";
126
+
127
+
128
+
129
+ try {
130
+
131
+ // 接続処理
132
+
133
+ conn = DriverManager.getConnection(url, user, password);
134
+
135
+
136
+
137
+ // ■検索処理実行
138
+
139
+ // SQL作成
140
+
141
+ String sql = "SELECT coursea_value, courseb_value, coursec_value,opcoursea_value FROM seisan";
142
+
143
+ System.out.println(sql);
144
+
145
+
146
+
147
+ // SQL準備
148
+
149
+ ps = conn.prepareStatement(sql);
150
+
151
+
152
+
153
+ // SQL実行
154
+
155
+ rs = ps.executeQuery();
156
+
157
+
158
+
159
+ // データを読込み、JavaBeansに設定する
160
+
161
+ ryoukin = new ryoukin_beans(0, 0, 0, 0, 0, 0);
162
+
163
+ while(rs.next()) {
164
+
165
+ // データ読込
166
+
167
+ int coursea = rs.getInt("coursea_value");
168
+
169
+ int courseb = rs.getInt("courseb_value");
170
+
171
+ int coursec = rs.getInt("coursec_value");
172
+
173
+ int opcoursea= rs.getInt("opcoursea_value");
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+ // JavaBeansにデータを設定
182
+
183
+ ryoukin.set(coursea);
184
+
185
+ ryoukin.set(courseb);
186
+
187
+ ryoukin.set(coursec);
188
+
189
+ ryoukin.set(opcoursea);
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ }
198
+
199
+ System.out.println(ryoukin);
200
+
201
+ } catch(SQLException e) {
202
+
203
+ // 接続失敗時の処理
204
+
205
+ e.printStackTrace();
206
+
207
+
208
+
209
+ } finally {
210
+
211
+ try {
212
+
213
+ // ■クローズ処理
214
+
215
+ // 検索結果
216
+
217
+ if(rs!=null) {
218
+
219
+ rs.close();}
220
+
221
+
222
+
223
+ // SQL文
224
+
225
+ if(ps!=null) {
226
+
227
+ ps.close();
228
+
229
+ }
230
+
231
+ // DB接続
232
+
233
+ if(conn!=null) {
234
+
235
+ conn.close();
236
+
237
+ }
238
+
239
+ } catch (SQLException e) {
240
+
241
+ e.printStackTrace();
242
+
243
+ }
244
+
245
+ }
246
+
247
+
248
+
249
+ // 【表示値を設定する】
250
+
251
+ request.setAttribute("ryoukin", ryoukin);
252
+
253
+
254
+
255
+ // 【画面に遷移する】
256
+
257
+ request.getRequestDispatcher("Newryoukin.jsp").forward(request, response);
258
+
259
+ }
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+ /**
268
+
269
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
270
+
271
+ */
272
+
273
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
274
+
275
+ // TODO Auto-generated method stub
276
+
277
+ doGet(request, response);
278
+
279
+ // 【文字化け対応】
280
+
281
+ response.setContentType("text/html; charset=UTF-8");
282
+
283
+ response.setContentType("text/html; charset=UTF-8");
284
+
285
+
286
+
287
+ // 【画面入力値を取得し、料金情報に設定する】
288
+
289
+ // 画面入力値を取得
290
+
291
+ String course = request.getParameter("coursea_value");
292
+
293
+ int coursea = Integer.parseInt(course);
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+ // 取得できたか確認
302
+
303
+ System.out.println("coursea="+course);}
46
304
 
47
305
  }
48
306
 
49
- public int getCoursea_value() {
307
+
50
-
51
- return coursea_value;
308
+
52
-
53
- }
309
+
54
-
55
- public void setCoursea_value(int coursea_value) {
310
+
56
-
57
- this.coursea_value = coursea_value;
311
+
58
-
59
- }
312
+
60
-
61
- public int getCourseb_value() {
313
+
62
-
63
- return courseb_value;
314
+
64
-
65
- }
315
+
66
-
67
- public void setCourseb_value(int courseb_value) {
316
+
68
-
69
- this.courseb_value = courseb_value;
317
+
70
-
71
- }
318
+
72
-
73
- public int getCoursec_value() {
319
+
74
-
75
- return coursec_value;
76
-
77
- }
78
-
79
- public void setCoursec_value(int coursec_value) {
80
-
81
- this.coursec_value = coursec_value;
82
-
83
- }
84
-
85
- public int getOpcoursea_value() {
86
-
87
- return opcoursea_value;
88
-
89
- }
90
-
91
- public void setOpcoursea_value(int opcoursea_value) {
92
-
93
- this.opcoursea_value = opcoursea_value;
94
-
95
- }
96
-
97
- public int getOpcourseb_value() {
98
-
99
- return opcourseb_value;
100
-
101
- }
102
-
103
- public void setOpcourseb_value(int opcourseb_value) {
104
-
105
- this.opcourseb_value = opcourseb_value;
106
-
107
- }
108
-
109
- public int getOpcoursec_value() {
110
-
111
- return opcoursec_value;
112
-
113
- }
114
-
115
- public void setOpcoursec_value(int opcoursec_value) {
116
-
117
- this.opcoursec_value = opcoursec_value;
118
-
119
- }
120
-
121
- @Override
122
-
123
- public String toString() {
124
-
125
- return "ryoukin_beans [coursea_value=" + coursea_value + ", courseb_value=" + courseb_value + ", coursec_value="
126
-
127
- + coursec_value + ", opcoursea_value=" + opcoursea_value + ", opcourseb_value=" + opcourseb_value
128
-
129
- + ", opcoursec_value=" + opcoursec_value + "]";
130
-
131
- }
132
-
133
- public void set(int coursea) {
134
-
135
- // TODO 自動生成されたメソッド・スタブ
136
-
137
-
138
-
139
- }
140
-
141
-
142
-
143
-
144
-
145
- }
146
320
 
147
321
 
148
322