teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追加

2020/03/19 02:05

投稿

bwrs1
bwrs1

スコア9

title CHANGED
File without changes
body CHANGED
@@ -18,6 +18,7 @@
18
18
  ---
19
19
  - 現状出来ている事
20
20
  DBに接続は出来ています
21
+ JSP → サーブレットの間での受け渡しも出来ています。
21
22
 
22
23
 
23
24
 

1

追加

2020/03/19 02:05

投稿

bwrs1
bwrs1

スコア9

title CHANGED
File without changes
body CHANGED
@@ -10,6 +10,19 @@
10
10
 
11
11
  宜しくお願いします。
12
12
 
13
+ ---
14
+
15
+ - やってみたこと
16
+ DBUtils を使用してサーブレットに渡した値をDBにデータの追加できるか
17
+
18
+ ---
19
+ - 現状出来ている事
20
+ DBに接続は出来ています
21
+
22
+
23
+
24
+ JSP
25
+
13
26
  ``````ここに言語を入力
14
27
  <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
15
28
  <!DOCTYPE html>
@@ -178,169 +191,122 @@
178
191
  ```
179
192
  サーブレット
180
193
 
181
- ``````ここに言語を入力
182
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
183
- <!DOCTYPE html>
194
+ ```package MapDraw;
184
195
 
185
- <!--
186
- 入力フォームで入力されたデータを
187
- JSP,サーブレット,DBの流れで
196
+ import java.io.IOException;
188
- データの受け渡しをする
197
+ import java.io.PrintWriter;
189
- -->
190
198
 
199
+ import javax.servlet.RequestDispatcher;
200
+ import javax.servlet.ServletException;
201
+ import javax.servlet.annotation.WebServlet;
191
- <html>
202
+ import javax.servlet.http.HttpServlet;
203
+ import javax.servlet.http.HttpServletRequest;
204
+ import javax.servlet.http.HttpServletResponse;
192
205
 
193
- <head>
194
- <meta charset="UTF-8">
195
- <title>編集画面</title>
196
206
 
207
+ /**
197
- <!--CSS 読み込み-->
208
+ * Servlet implementation class unti
209
+ */
198
- <link rel="stylesheet" href="EditingScreen.css">
210
+ @WebServlet("/JspSendToServlet")
211
+ public class JspSendToServlet extends HttpServlet {
212
+ private static final long serialVersionUID = 1L;
199
213
 
214
+ /**
215
+ * @see HttpServlet#HttpServlet()
216
+ */
200
- <!--JS 読み込み-->
217
+ public JspSendToServlet() {
218
+ super();
201
- <script type="text/javascript" src="EditingScreen.js"></script>
219
+ // TODO Auto-generated constructor stub
220
+ }
202
221
 
222
+ /**
223
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
224
+ */
225
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
226
+ throws ServletException, IOException {
203
- <!--jQuery 読み込み-->
227
+ // TODO Auto-generated method stub
204
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
228
+ String view = "/WebContent/jsp/EditingScreen.jsp";
229
+ RequestDispatcher dispatch = request.getRequestDispatcher(view);
205
230
 
206
- <!--jQuery UI-->
231
+ //②htmlを出力
207
- <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
232
+ PrintWriter out = response.getWriter();
208
- <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script>
209
233
 
210
- <!--JS 読み込み-->
234
+ }
211
- <script src="${pageContext.request.contextPath}/js/EditingScreen.js"></script>
212
235
 
213
- <!--CSS 読み込み-->
236
+ /**
214
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/EditingScreen.css">
237
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
238
+ */
239
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
240
+ throws ServletException, IOException {
241
+ // TODO Auto-generated method stub
242
+ doGet(request, response);
215
243
 
216
- <!--datapircker 着工年月日-->
217
- <script>
218
- $(function() {
219
- $("#genChakkYmd").datepicker();
244
+ request.setCharacterEncoding("UTF-8");
220
- });
221
- </script>
222
245
 
223
- <!--datapircker 竣工年月日-->
224
- <script>
225
- $(function() {
226
- $("#genShunkYmd").datepicker();
227
- });
228
- </script>
246
+ //キーワード取得
247
+ String va1 = request.getParameter("genName");
248
+ String va2 = request.getParameter("genAddr");
249
+ String va3 = request.getParameter("genChakkYmd");
250
+ String va4 = request.getParameter("tantoName");
251
+ String va5 = request.getParameter("genTel");
252
+ String va6 = request.getParameter("genShunkYmd");
253
+ String va7 = request.getParameter("genFax");
229
254
 
230
- </head>
255
+ System.out.println(va1);
256
+ System.out.println(va2);
257
+ System.out.println(va3);
258
+ System.out.println(va4);
259
+ System.out.println(va5);
260
+ System.out.println(va6);
261
+ System.out.println(va7);
231
262
 
232
- <body>
263
+ }
233
- <!-- 入力フォーム -->
264
+ }
234
- <form method="POST" action="/mapDrawJava/JspSendToServlet" name="form1" onreset="return confirm('内容をリセットしてもよろしいですか?') ;">
235
265
 
236
- <!-- 一行目 START-->
237
266
 
238
- <!--現場名ボタン-->
267
+ ```
239
- <label for="genName">現場名 </label>
240
- <input type="text" name="genName" id="genName" maxlength="40">
241
268
 
242
- <!--住所ボタン-->
269
+ ConnectionDB
243
- <label for="genAddr"> 住所   </label>
244
- <input type="text" name="genAddr" id="genAddr" maxlength="80">
245
270
 
246
- <!--着工年月日ボタン-->
271
+ ```package MapDraw;
247
- <label for="genChakkYmd">着工年月日 </label>
248
- <input type="text" name="genChakkYmd" id="genChakkYmd">
249
272
 
250
- <br><br>
273
+ import java.sql.Connection;
274
+ import java.sql.DriverManager;
275
+ import java.sql.SQLException;
251
276
 
277
+ /*
278
+ * MySQL に接続できているか確認するだけのソース
279
+ **/
252
280
 
253
- <!-- 一行目 END-->
281
+ public class MysqlConnectionCheck {
282
+ public static void main(String[] args) {
254
283
 
255
- <!-- 二行目 START-->
256
284
 
257
- <!--担当者 プルダウンボタン-->
285
+ Connection con = null;
258
- <label for="tantoName">担当者 </label>
259
286
 
260
- <!-- プルダウンメニュー 一覧 START-->
261
- <select name="tantoName" id="tantoName">
262
- <option value="">担当者一覧</option>
263
- <option value="a">どらえもん</option>
264
- <option value="b">のび太</option>
265
- <option value="c">しずかちゃん</option>
266
- <option value="d">ジャイアン</option>
267
- <option value="e">スネ夫</option>
268
- </select>
287
+ try {
269
- <!-- プルダウンメニュー 一覧 END-->
270
288
 
271
- <!--電話番号ボタン-->
289
+ //接続するMySQL DB
272
- <label for="genTel">電話番号  </label>
273
- <input type="tel" name="genTel" id="genTel">
290
+ con = DriverManager.getConnection("jdbc:mysql://ServerName:port/データベース名", "ユーザー名", "パスワード");
274
291
 
275
- <!--竣工年月日ボタン-->
276
- <label for="genShunkYmd">竣工年月日 </label>
292
+ System.out.println("MySQLに接続できました。");
277
- <input type="text" name="genShunkYmd" autocomplete="off" id="genShunkYmd">
278
293
 
294
+ } catch (SQLException e) {
279
295
 
296
+ System.out.println("MySQLに接続できませんでした。");
280
297
 
298
+ } finally {
281
- <!-- 二行目 END-->
299
+ if (con != null) {
300
+ try {
301
+ con.close();
302
+ } catch (SQLException e) {
303
+ System.out.println("MySQLのクローズに失敗しました。");
304
+ }
305
+ }
306
+ }
282
307
 
283
- <br><br>
308
+ }
309
+ }
284
310
 
285
- <!-- 三行目 START-->
286
-
287
- <!--FAXボタン-->
288
- <label for="genFax">                  Fax  <input type="text" name="genFax" id="genFax"></label>
289
-
290
-
291
- <!--工期表示 領域-->
292
- <label for="ConstructionPeriod"> 工期 </label>
293
- <label for="ConstructionPeriod"> ○○ヵ月</label>
294
-
295
-
296
- <!-- 三行目 END-->
297
-
298
- <!-- mapdrawツールの上の図形のラベル -->
299
- <p>地図:</p>
300
-
301
- <!-- mapdrawボックス START-->
302
- <table border="1" cellspacing="0" cellpadding="5"
303
- bordercolor="#000000" style="border-collapse: collapse">
304
- <tr>
305
- <td>矩形</td>
306
- <td>円</td>
307
- <td>ポリゴン</td>
308
- <td>ポリライン</td>
309
- <td style="width: 70px; "></td>
310
- <td>     </td>
311
- <td>    </td>
312
- </tr>
313
- </table>
314
- <!-- mapdrawボックス END-->
315
-
316
- <!-- mapdrawツールとmapdrawツールパレットを並べて表示 -->
317
- <div class="flex_box">
318
-
319
- <!-- mapdrawツール CLASS-->
320
- <div class="mapdraw">mapdrawツール</div>
321
-
322
- <!-- mapdrawツールパレットボックス START -->
323
- <div class="mapdrawPallet">mapdrawツールパレット</div>
324
- <!-- mapdrawツールパレットボックス END -->
325
-
326
- </div>
327
-
328
- <!-- 削除ボタンと登録ボタン START-->
329
- <div class="btn1">
330
- <p>
311
+ コード
331
-
332
- <!-- 登録ボタン -->
333
- <input type="submit" value="登録" onclick="return submitCheck()">
334
-
335
- <!-- 削除ボタン-->
336
- <input type="reset" value="削除" onclick="return resetCheck()">
337
-
338
- </p>
339
- <!-- 削除ボタンと登録ボタン END-->
340
-
341
- </div>
342
- </form>
343
- </body>
344
-
345
- </html>
346
312
  ```