質問編集履歴
1
作成途中で誤って投稿したため修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,7 +24,113 @@
|
|
24
24
|
|
25
25
|
```Java
|
26
26
|
|
27
|
+
① AccountDAO.java
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
package dao;
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
import java.sql.Connection;
|
36
|
+
|
37
|
+
import java.sql.DriverManager;
|
38
|
+
|
39
|
+
import java.sql.PreparedStatement;
|
40
|
+
|
41
|
+
import java.sql.ResultSet;
|
42
|
+
|
43
|
+
import java.sql.SQLException;
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
import model.Account;
|
48
|
+
|
49
|
+
import model.Login;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
public class AccountDAO {
|
54
|
+
|
55
|
+
// データベース接続に使用する情報
|
56
|
+
|
57
|
+
private final String JDBC_URL = "jdbc:oracle:thin:[ユーザー名]/[パスワード]@//localhost:1521/orcl";
|
58
|
+
|
59
|
+
private final String DB_USER = "[ユーザー名]";
|
60
|
+
|
61
|
+
private final String DB_PASS = "[パスワード]";
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
public Account findByLogin(Login login) {
|
66
|
+
|
67
|
+
Account account = null;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
// データベースへ接続
|
72
|
+
|
73
|
+
try (Connection conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS)) {
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
// SELECT文を準備
|
78
|
+
|
79
|
+
String sql = "SELECT USER_ID, PASS, HEIGHT, WEIGHT_START, WEIGHT_GOAL FROM ACCOUNT WHERE USER_ID = ? AND PASS = ?";
|
80
|
+
|
81
|
+
PreparedStatement pStmt = conn.prepareStatement(sql);
|
82
|
+
|
83
|
+
pStmt.setString(1, login.getUserId());
|
84
|
+
|
85
|
+
pStmt.setString(2, login.getPass());
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
// SELECT文を実行し、結果表を取得
|
90
|
+
|
91
|
+
ResultSet rs = pStmt.executeQuery();
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
// 一致したユーザーが存在した場合、そのユーザーを表すAccountインスタンスを生成
|
96
|
+
|
97
|
+
if (rs.next()) {
|
98
|
+
|
99
|
+
// 結果表からデータを取得
|
100
|
+
|
101
|
+
String userId = rs.getString("USER_ID");
|
102
|
+
|
103
|
+
String pass = rs.getString("PASS");
|
104
|
+
|
105
|
+
double height = rs.getDouble("HEIGHT");
|
106
|
+
|
107
|
+
double weight_start = rs.getDouble("WEIGHT_START");
|
108
|
+
|
109
|
+
double weight_goal = rs.getDouble("WEIGHT_GOAL");
|
110
|
+
|
111
|
+
account = new Account(userId, pass, height, weight_start, weight_goal);
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
} catch (SQLException e) {
|
116
|
+
|
117
|
+
e.printStackTrace();
|
118
|
+
|
119
|
+
return null;
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
// 見つかったユーザーまたはnullを返す
|
124
|
+
|
125
|
+
return account;
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
27
|
-
|
133
|
+
② AccountDAOTest.java
|
28
134
|
|
29
135
|
|
30
136
|
|
@@ -60,11 +166,11 @@
|
|
60
166
|
|
61
167
|
if (result != null) {
|
62
168
|
|
63
|
-
System.out.println("testFindByLogin
|
169
|
+
System.out.println("testFindByLogin:成功しました");
|
64
170
|
|
65
171
|
} else {
|
66
172
|
|
67
|
-
System.out.println("testFindByLogin
|
173
|
+
System.out.println("testFindByLogin:失敗しました");
|
68
174
|
|
69
175
|
}
|
70
176
|
|
@@ -74,6 +180,98 @@
|
|
74
180
|
|
75
181
|
|
76
182
|
|
183
|
+
③ Account.java
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
package model;
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
public class Account {
|
192
|
+
|
193
|
+
private String userId;
|
194
|
+
|
195
|
+
private String pass;
|
196
|
+
|
197
|
+
private double height;
|
198
|
+
|
199
|
+
private double weight_start;
|
200
|
+
|
201
|
+
private double weight_goal;
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
// コンストラクタを設定
|
206
|
+
|
207
|
+
public Account(String userId, String pass, double height, double weight_start, double weight_goal) {
|
208
|
+
|
209
|
+
this.userId = userId;
|
210
|
+
|
211
|
+
this.pass = pass;
|
212
|
+
|
213
|
+
this.height = height;
|
214
|
+
|
215
|
+
this.weight_start = weight_start;
|
216
|
+
|
217
|
+
this.weight_goal = weight_goal;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
// ゲッターを設定
|
224
|
+
|
225
|
+
public String getUserId() { return userId; }
|
226
|
+
|
227
|
+
public String getPass() { return pass; }
|
228
|
+
|
229
|
+
public double getHeight() { return height; }
|
230
|
+
|
231
|
+
public double getWeight_start() { return weight_start; }
|
232
|
+
|
233
|
+
public double getWeight_goal() { return weight_goal; }
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
④ Login.java
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
package model;
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
public class Login {
|
250
|
+
|
251
|
+
private String userId;
|
252
|
+
|
253
|
+
private String pass;
|
254
|
+
|
255
|
+
// コンストラクタ(インスタンス生成時に呼び出されるメソッド)を設定
|
256
|
+
|
257
|
+
public Login(String userId, String pass) {
|
258
|
+
|
259
|
+
this.userId = userId;
|
260
|
+
|
261
|
+
this.pass = pass;
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
// ゲッター(クラス外からフィールドの値を見れるメソッド)を設定
|
266
|
+
|
267
|
+
public String getUserId() { return userId; }
|
268
|
+
|
269
|
+
public String getPass() { return pass; }
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
|
77
275
|
```
|
78
276
|
|
79
277
|
|
@@ -82,7 +280,9 @@
|
|
82
280
|
|
83
281
|
|
84
282
|
|
283
|
+
「スッキリわかるサーブレット&JSP」というテキストを参考に作成しており、コードを見直していますが、どこに問題があるのかわかりません。
|
284
|
+
|
85
|
-
|
285
|
+
DB接続エラーなどは出ていません。
|
86
286
|
|
87
287
|
|
88
288
|
|
@@ -90,4 +290,6 @@
|
|
90
290
|
|
91
291
|
|
92
292
|
|
93
|
-
|
293
|
+
Toncat9(Java11)
|
294
|
+
|
295
|
+
Oracle12c
|