質問編集履歴
2
あああああああああ
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
したいああああああああああああ
|
test
CHANGED
File without changes
|
1
消去しました
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
サーブレット、JSPで商品検索したい
|
1
|
+
サーブレット、JSPで商品検索したいああああああああああああ
|
test
CHANGED
@@ -1,289 +1,2 @@
|
|
1
|
-
### 前提
|
2
|
-
|
1
|
+
まちがえて投稿してしまったため、質問を消去しました。
|
3
|
-
|
4
|
-
|
2
|
+
ああああああああああああああああああああああああああああああああああああああああ
|
5
|
-
eclipsを起動させてPCと入力し、検索を押下しても商品コード、商品名、単価、操作のセルに何も表示されません
|
6
|
-
|
7
|
-
### 発生している問題・エラーメッセージ
|
8
|
-
|
9
|
-
```
|
10
|
-
エラーメッセージ
|
11
|
-
何もないです
|
12
|
-
```
|
13
|
-
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-08-31/bca9e84b-9eb3-41a7-9bbb-c62a86010858.png)
|
14
|
-
|
15
|
-
### 該当のソースコード
|
16
|
-
|
17
|
-
```java servlet
|
18
|
-
package servlet;
|
19
|
-
|
20
|
-
import java.io.IOException;
|
21
|
-
import java.util.ArrayList;
|
22
|
-
|
23
|
-
import javax.servlet.RequestDispatcher;
|
24
|
-
import javax.servlet.ServletException;
|
25
|
-
import javax.servlet.annotation.WebServlet;
|
26
|
-
import javax.servlet.http.HttpServlet;
|
27
|
-
import javax.servlet.http.HttpServletRequest;
|
28
|
-
import javax.servlet.http.HttpServletResponse;
|
29
|
-
|
30
|
-
import bean.BkadaiBean;
|
31
|
-
import controller.BkadaiController3_1;
|
32
|
-
|
33
|
-
@WebServlet("/servlet/kensaku3-1")
|
34
|
-
public class BkadaiServlet3_1 extends HttpServlet {
|
35
|
-
|
36
|
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
37
|
-
throws ServletException, IOException {
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
try {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
//パラメータnameの値取得
|
47
|
-
String name = request.getParameter("name");
|
48
|
-
|
49
|
-
//配列宣言
|
50
|
-
ArrayList<BkadaiBean> list = new ArrayList<BkadaiBean>();
|
51
|
-
|
52
|
-
/*
|
53
|
-
*nameを元に、商品のを検索する関数の呼び出し、結果をJSPに渡す処理
|
54
|
-
*/
|
55
|
-
|
56
|
-
//BkadaiController3_1クラスをインスタンス化する。
|
57
|
-
BkadaiController3_1 bkadaiController1 = new BkadaiController3_1();
|
58
|
-
|
59
|
-
// BkadaiBeanに、BkadaiController3_1よりsearch関数を呼び出し、全検索メソッド実行
|
60
|
-
list = bkadaiController1.search(name);
|
61
|
-
|
62
|
-
//検索結果を持ってkensaku3-1.jspにフォワード
|
63
|
-
request.setAttribute("kensaku3-1", list);
|
64
|
-
|
65
|
-
} catch (IllegalStateException e) {
|
66
|
-
e.printStackTrace();
|
67
|
-
|
68
|
-
} catch (Exception e) {
|
69
|
-
e.printStackTrace();
|
70
|
-
|
71
|
-
// kensaku3-1へ
|
72
|
-
}finally{
|
73
|
-
//フォワード先の指定
|
74
|
-
RequestDispatcher list = request.getRequestDispatcher("/kensaku3-1.jsp");
|
75
|
-
|
76
|
-
//フォワードの実行
|
77
|
-
list.forward(request, response);
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
|
82
|
-
}
|
83
|
-
|
84
|
-
}
|
85
|
-
|
86
|
-
```
|
87
|
-
```ここに言語を入力
|
88
|
-
package controller;
|
89
|
-
|
90
|
-
import java.sql.Connection;
|
91
|
-
import java.sql.DriverManager;
|
92
|
-
import java.sql.ResultSet;
|
93
|
-
import java.sql.SQLException;
|
94
|
-
import java.sql.Statement;
|
95
|
-
import java.util.ArrayList;
|
96
|
-
|
97
|
-
import bean.BkadaiBean;
|
98
|
-
|
99
|
-
public class BkadaiController3_1 {
|
100
|
-
|
101
|
-
private static String RDB_DRIVE = "com.mysql.cj.jdbc.Driver";
|
102
|
-
private static String URL = "jdbc:mysql://localhost/bkadai";
|
103
|
-
private static String USER = "root";
|
104
|
-
private static String PASS = "tsukasa617";
|
105
|
-
|
106
|
-
//データベース接続を行うメソッド
|
107
|
-
public static Connection getConnection() {
|
108
|
-
try {
|
109
|
-
Class.forName(RDB_DRIVE);
|
110
|
-
Connection con = DriverManager.getConnection(URL, USER, PASS);
|
111
|
-
return con;
|
112
|
-
} catch (Exception e) {
|
113
|
-
throw new IllegalStateException(e);
|
114
|
-
}
|
115
|
-
}
|
116
|
-
|
117
|
-
// 送信された商品名を元に商品コード、商品名、単価を検索するためのメソッド
|
118
|
-
public ArrayList<BkadaiBean> search(String name) {
|
119
|
-
|
120
|
-
//変数宣言
|
121
|
-
Connection con = null;
|
122
|
-
Statement statement = null;
|
123
|
-
|
124
|
-
//return用オブジェクトの生成
|
125
|
-
ArrayList<BkadaiBean> list = new ArrayList<BkadaiBean>();
|
126
|
-
|
127
|
-
// 商品検索
|
128
|
-
String sql = "select * from m_product where product_name like '%" + name + "%'";
|
129
|
-
|
130
|
-
try {
|
131
|
-
con = getConnection();
|
132
|
-
statement = con.createStatement();
|
133
|
-
|
134
|
-
//SQLをDBへ発行
|
135
|
-
ResultSet rs = statement.executeQuery(sql);
|
136
|
-
|
137
|
-
//検索結果を配列に格納
|
138
|
-
while (rs.next()) {
|
139
|
-
|
140
|
-
BkadaiBean accountinfo = new BkadaiBean();
|
141
|
-
accountinfo.setNum(rs.getInt("product_code"));
|
142
|
-
accountinfo.setName(rs.getString("product_name"));
|
143
|
-
accountinfo.setPrice(rs.getInt("price"));
|
144
|
-
list.add(accountinfo);
|
145
|
-
}
|
146
|
-
|
147
|
-
} catch (Exception e) {
|
148
|
-
throw new IllegalStateException(e);
|
149
|
-
} finally {
|
150
|
-
// 接続の切断
|
151
|
-
if (statement != null) {
|
152
|
-
try {
|
153
|
-
statement.close();
|
154
|
-
} catch (SQLException ignore) {
|
155
|
-
}
|
156
|
-
}
|
157
|
-
if (con != null) {
|
158
|
-
try {
|
159
|
-
con.close();
|
160
|
-
} catch (SQLException ignore) {
|
161
|
-
}
|
162
|
-
}
|
163
|
-
}
|
164
|
-
return list;
|
165
|
-
}
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
```
|
170
|
-
|
171
|
-
```JSP
|
172
|
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
173
|
-
pageEncoding="UTF-8"%>
|
174
|
-
<%@ page import="bean.BkadaiBean"%>
|
175
|
-
<%@page import="java.util.ArrayList,bean.BkadaiBean"%>
|
176
|
-
<%
|
177
|
-
ArrayList<BkadaiBean> list = (ArrayList<BkadaiBean>)request.getAttribute("list");
|
178
|
-
String error = (String)request.getAttribute("error");
|
179
|
-
%>
|
180
|
-
|
181
|
-
<!DOCTYPE html>
|
182
|
-
<html>
|
183
|
-
<head>
|
184
|
-
<meta charset="UTF-8">
|
185
|
-
<title>商品検索</title>
|
186
|
-
</head>
|
187
|
-
<body>
|
188
|
-
<h1>商品検索</h1>
|
189
|
-
|
190
|
-
<br />
|
191
|
-
<p>商品名</p>
|
192
|
-
<form action="<%=request.getContextPath() %>/servlet/kensaku3-1">
|
193
|
-
<input type="text" name="name"> <input type="submit"
|
194
|
-
value="検索"><br />
|
195
|
-
|
196
|
-
|
197
|
-
<table>
|
198
|
-
<tr>
|
199
|
-
<th>商品コード</th>
|
200
|
-
<th>商品名</th>
|
201
|
-
<th>単価</th>
|
202
|
-
<th>操作</th>
|
203
|
-
</tr>
|
204
|
-
|
205
|
-
<%
|
206
|
-
if(list != null){
|
207
|
-
for(int i=0;i<list.size();i++){
|
208
|
-
%>
|
209
|
-
<tr>
|
210
|
-
<td><%= list.get(i).getName() %></td>
|
211
|
-
<td><%= list.get(i).getNum() %></td>
|
212
|
-
<td><%= list.get(i).getPrice() %></td>
|
213
|
-
<td><a href="A.html">編集</a></td>
|
214
|
-
</tr>
|
215
|
-
<%
|
216
|
-
}
|
217
|
-
}
|
218
|
-
%>
|
219
|
-
|
220
|
-
</table>
|
221
|
-
|
222
|
-
|
223
|
-
</form>
|
224
|
-
</body>
|
225
|
-
</html>
|
226
|
-
```
|
227
|
-
```BEAN
|
228
|
-
package bean;
|
229
|
-
|
230
|
-
import java.io.Serializable;
|
231
|
-
|
232
|
-
public class BkadaiBean implements Serializable {
|
233
|
-
|
234
|
-
private String name;
|
235
|
-
private int num;
|
236
|
-
private int price;
|
237
|
-
private int number;
|
238
|
-
|
239
|
-
public BkadaiBean() {
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
// 商品名
|
244
|
-
public void setName(String name) {
|
245
|
-
this.name = name;
|
246
|
-
}
|
247
|
-
|
248
|
-
public String getName() {
|
249
|
-
return name;
|
250
|
-
}
|
251
|
-
|
252
|
-
// 商品コード
|
253
|
-
public void setNum(int num) {
|
254
|
-
this.num = num;
|
255
|
-
}
|
256
|
-
|
257
|
-
public int getNum() {
|
258
|
-
return num;
|
259
|
-
}
|
260
|
-
|
261
|
-
// 単価
|
262
|
-
public void setPrice(int price) {
|
263
|
-
this.price = price;
|
264
|
-
}
|
265
|
-
|
266
|
-
public int getPrice() {
|
267
|
-
return price;
|
268
|
-
}
|
269
|
-
|
270
|
-
// 数量
|
271
|
-
public void setNumber(int number) {
|
272
|
-
this.number = number;
|
273
|
-
}
|
274
|
-
|
275
|
-
public int getNumber() {
|
276
|
-
return number;
|
277
|
-
}
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
```
|
282
|
-
|
283
|
-
### 補足情報(FW/ツールのバージョンなど)
|
284
|
-
|
285
|
-
ここにより詳細な情報を記載してください。
|
286
|
-
java Ver17
|
287
|
-
mysql Ver 8.0.27
|
288
|
-
Eclips Ver 2022-06 (4.24.0)
|
289
|
-
Tomcat9
|