質問編集履歴

1

全体

2019/06/07 09:14

投稿

S_kura
S_kura

スコア12

test CHANGED
@@ -1 +1 @@
1
- jsp servlet でログイン処理の作成
1
+ java sqldbからデータ取得してログイン処理の作成
test CHANGED
@@ -1,20 +1,22 @@
1
- ### 前提・実現したいこと
1
+ #### 前提・実現したいこと
2
-
2
+
3
- kadai.javaにはsql接続しその文字列を出力実現るがそれらを
3
+ ログイン処理実現ため、入力された文字列をpostgresqlのdbで照合
4
-
4
+
5
- Sredirect.javaにひとつにまとめ画面上入力文字をsql照合し別画面へ飛ぶよな動作をさたい
5
+ そのためにtestjavaでsql接続・文字取得できいるか調べているですがまくできておりま
6
-
6
+
7
+
8
+
7
- ###いままで実現たこと
9
+ 発生てるエラーはなく、
8
-
10
+
9
- doPost()て入力した情報を受け取り、画面出力す
11
+ DAOjavaの取得したい列名(rs.getStringの中身)を適当なもの書き換え
10
-
11
-
12
-
12
+
13
- postgresのDB接続する
13
+ org.postgresql.util.PSQLException: こ ResultSet 列名 ●●●(書きかけた文字) ありません。
14
-
15
- SELECT文でデータを取得する
14
+
16
-
15
+
16
+
17
- 取得したデータが意図したデータか出力し確認
17
+ とでるのでsqldb取得まではできると思いま
18
+
19
+ そこから文字列比較しようとしてるところで失敗に終わるのでどうしてそうなっているのか、アドバイスしていただけると助かります。
18
20
 
19
21
 
20
22
 
@@ -22,59 +24,9 @@
22
24
 
23
25
 
24
26
 
25
- ```jsp
27
+ ```java
26
-
27
- <%@ page language="java" contentType="text/html; charset=UTF-8"
28
+
28
-
29
- pageEncoding="UTF-8"%>
30
-
31
-
32
-
33
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
34
-
35
- <html>
36
-
37
- <head>
38
-
39
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
40
-
41
- </head>
42
-
43
- <title>サンプル</title>
44
-
45
- </head>
46
-
47
- <body>
48
-
49
- <p>ログイン画面</p>
50
-
51
- <form action="Sredirect" method="get">
52
-
53
- <p>ID:<input type="text" name="word"></p>
54
-
55
- <form action="Sredirect" method="get">
56
-
57
- <p>パス:<input type ="text" name="pass"></p>
58
-
59
-
60
-
61
- <p><input type="submit" value="ログイン" style="WIDTH: 200px; HEIGHT: 20px"></p>
62
-
63
- </form></form>
64
-
65
- </body>
66
-
67
- </html>
68
-
69
- ```
70
-
71
- ```sql接続から書き出しjava
72
-
73
- ソースコード
74
-
75
- package web1;
29
+ package dao;
76
-
77
-
78
30
 
79
31
 
80
32
 
@@ -82,289 +34,337 @@
82
34
 
83
35
  import java.sql.DriverManager;
84
36
 
37
+ import java.sql.PreparedStatement;
38
+
85
39
  import java.sql.ResultSet;
86
40
 
87
- import java.sql.ResultSetMetaData;
88
-
89
41
  import java.sql.SQLException;
90
42
 
91
- import java.sql.Statement;
92
-
93
- import java.util.ArrayList;
94
-
95
- import java.util.List;
96
-
97
-
98
-
99
- public class kadai {
100
-
101
-
102
-
103
- public static void main(String[] args) {
104
-
105
-
106
-
107
-
108
-
109
- Connection conn = null;
110
-
111
- Statement stmt = null;
112
-
113
- ResultSet rset = null;
114
-
115
-
116
-
117
- //接続文字列
118
-
119
- String url = "jdbc:postgresql://127.0.0.1:5432/test";
120
-
121
- String user = "postgres";
122
-
123
- String password = "postgres";
124
-
125
-
126
-
127
- try{
128
-
129
- //PostgreSQLへ接続
130
-
131
- conn = DriverManager.getConnection(url, user, password);
132
-
133
-
134
-
135
- //自動コミットOFF
136
-
137
- conn.setAutoCommit(false);
138
-
139
-
140
-
141
- //SELECT文の実行
142
-
143
- stmt = conn.createStatement();
144
-
145
- String sql = "SELECT * FROM ユーザーログイン情報";
146
-
147
- rset = stmt.executeQuery(sql);
148
-
149
-
150
-
151
- //フィルド一覧を取得
152
-
153
- List<String> fields = new ArrayList<String>();
154
-
155
- ResultSetMetaData rsmd = rset.getMetaData();
156
-
157
- for(int i = 1;i <= rsmd.getColumnCount(); i++) {
158
-
159
- fields.add(rsmd.getColumnName(i));
160
-
161
- }
162
-
163
- //SELECT結果の受け取り
164
-
165
- int row =0;
166
-
167
- while (rset.next()) {
168
-
169
- row++;
170
-
171
-
172
-
173
- for(String field :fields) {
174
-
175
-
176
-
177
- System.out.println(
178
-
179
- field+":"+
180
-
181
- rset.getString(field));
182
-
183
- }
184
-
185
-
186
-
187
- }
188
-
189
- }
190
-
191
- catch (SQLException e){
192
-
193
- e.printStackTrace();
194
-
195
- }
196
-
197
- finally {
198
-
199
- try {
200
-
201
- if(rset != null)rset.close();
202
-
203
- if(stmt != null)stmt.close();
204
-
205
- if(conn != null)conn.close();
206
-
207
- }
208
-
209
- catch (SQLException e){
210
-
211
- e.printStackTrace();
212
-
213
- }
214
-
215
-
216
-
217
- }
43
+
44
+
45
+ import model.Account;
46
+
47
+ import model.Login;
48
+
49
+
50
+
51
+ public class AccountDAO {
52
+
53
+ // データベース接続に使用する情報
54
+
55
+ private final String url = "jdbc:postgresql://127.0.0.1:5432/test";
56
+
57
+ //private final String url ="jdbc:postgresql://127.0.0.1:5432/";
58
+
59
+ private final String user = "postgres";
60
+
61
+ private final String password = "postgres";
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(
74
+
75
+ url, user, password)) {
76
+
77
+
78
+
79
+ // SELECT文を準備
80
+
81
+ String sql = "SELECT USER_ID, PASS, MAIL, NAME, AGE FROM ACCOUNT WHERE USER_ID = ? AND PASS = ?";
82
+
83
+ PreparedStatement pStmt = conn.prepareStatement(sql);
84
+
85
+ pStmt.setString(1, login.getUserId());
86
+
87
+ pStmt.setString(2, login.getPass());
88
+
89
+
90
+
91
+ // SELECTを実行し、結果表を取得
92
+
93
+ ResultSet rs = pStmt.executeQuery();
94
+
95
+
96
+
97
+ // 一致したユーザーが存在した場合
98
+
99
+ // そのユーザーを表すAccountインスタンスを生成
100
+
101
+ if (rs.next()) {
102
+
103
+ // 結果表からデを取得
104
+
105
+ String userId = rs.getString("USER_ID");
106
+
107
+ String pass = rs.getString("PASS");
108
+
109
+ String mail = rs.getString("MAIL");
110
+
111
+ String name = rs.getString("NAME");
112
+
113
+ int age = rs.getInt("AGE");
114
+
115
+ account = new Account(userId, pass, mail, name, age);
116
+
117
+ }
118
+
119
+ } catch (SQLException e) {
120
+
121
+ e.printStackTrace();
122
+
123
+ return null;
218
124
 
219
125
  }
220
126
 
127
+ // 見つかったユーザーまたはnullを返す
128
+
129
+ return null;
130
+
131
+ }
132
+
221
133
  }
222
134
 
223
- ```
224
-
225
- ```リダイレクト.jsp
226
-
227
- package web1;
228
-
229
-
230
-
231
-
232
-
233
- import java.io.IOException;
234
-
235
- import java.io.PrintWriter;
236
-
237
- import java.sql.Connection;
238
-
239
- import java.sql.DriverManager;
240
-
241
- import java.sql.ResultSet;
242
-
243
- import java.sql.ResultSetMetaData;
244
-
245
- import java.sql.Statement;
246
-
247
- import java.util.ArrayList;
248
-
249
- import java.util.List;
250
-
251
-
252
-
253
- import javax.servlet.RequestDispatcher;
254
-
255
- import javax.servlet.ServletContext;
256
-
257
- import javax.servlet.ServletException;
258
-
259
- import javax.servlet.annotation.WebServlet;
260
-
261
- import javax.servlet.http.HttpServlet;
262
-
263
- import javax.servlet.http.HttpServletRequest;
264
-
265
- import javax.servlet.http.HttpServletResponse;
266
-
267
-
268
-
269
- @WebServlet("/Sredirect")//[1]
270
-
271
- public class Sredirect extends HttpServlet {//[2]
272
-
273
-
274
-
275
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
276
-
277
- throws ServletException, IOException {//[3]
278
-
279
-
280
-
281
- response.setContentType("text/html; charset=UTF-8");//[4]
282
-
283
-
284
-
285
- //入力された単語を取得したのを変数に代入
286
-
287
- String userid = request.getParameter("word");
288
-
289
- String pass = request.getParameter("pass");
290
-
291
- String loginR ="";
292
-
293
- if (!checkWord(userid,pass)) {//[6]
294
-
295
-
296
-
297
-
298
-
299
- ServletContext context = this.getServletContext();
300
-
301
- RequestDispatcher dispatcher = context.getRequestDispatcher("/redirect.jsp");
302
-
303
- dispatcher.forward(request, response);
304
-
305
-
306
-
307
- return;//[8]
308
-
309
- }
310
-
311
-
312
-
313
- ServletContext context = this.getServletContext();
314
-
315
- RequestDispatcher dispatcher = context.getRequestDispatcher("/top.jsp");
316
-
317
- dispatcher.forward(request, response);
318
-
319
-
135
+
136
+
137
+ ```
138
+
139
+ ```java
140
+
141
+ package model;
142
+
143
+
144
+
145
+ public class Account {
146
+
147
+ private String userId;
148
+
149
+ private String pass;
150
+
151
+ private String mail;
152
+
153
+ private String name;
154
+
155
+ private int age;
156
+
157
+
158
+
159
+ public Account(String userId, String pass, String mail,
160
+
161
+ String name, int age) {
162
+
163
+ this.userId = userId;
164
+
165
+ this.pass = pass;
166
+
167
+ this.mail = mail;
168
+
169
+ this.name = name;
170
+
171
+ this.age = age;
172
+
173
+ }
174
+
175
+
176
+
177
+ public String getUserId() {
178
+
179
+ return userId;
180
+
181
+ }
182
+
183
+
184
+
185
+ public String getPass() {
186
+
187
+ return pass;
188
+
189
+ }
190
+
191
+
192
+
193
+ public String getMail() {
194
+
195
+ return mail;
196
+
197
+ }
198
+
199
+
200
+
201
+ public String getName() {
202
+
203
+ return name;
204
+
205
+ }
206
+
207
+
208
+
209
+ public int getAge() {
210
+
211
+ return age;
212
+
213
+ }
214
+
215
+ }
216
+
217
+
218
+
219
+ ```
220
+
221
+ ```java
222
+
223
+ package model;
224
+
225
+
226
+
227
+ public class Login {
228
+
229
+ private String userId;
230
+
231
+ private String pass;
232
+
233
+
234
+
235
+ public Login(String userId, String pass) {
236
+
237
+ this.userId = userId;
238
+
239
+ this.pass = pass;
240
+
241
+ }
242
+
243
+
244
+
245
+ public String getUserId() {
246
+
247
+ return userId;
248
+
249
+ }
250
+
251
+
252
+
253
+ public String getPass() {
254
+
255
+ return pass;
256
+
257
+ }
258
+
259
+ }
260
+
261
+ ```
262
+
263
+ ```java
264
+
265
+ package test;
266
+
267
+
268
+
269
+ import dao.AccountDAO;
270
+
271
+ import model.Account;
272
+
273
+ import model.Login;
274
+
275
+
276
+
277
+ public class AccountDAOTest {
278
+
279
+ public static void main(String[] args) {
280
+
281
+ testFindByLogin1(); // ユーザーが見つかる場合のテスト
282
+
283
+ testFindByLogin2(); // ユーザーが見つからない場合のテスト
284
+
285
+ }
286
+
287
+
288
+
289
+ public static void testFindByLogin1() {
290
+
291
+ Login login = new Login("toumasu", "1212");
292
+
293
+ AccountDAO dao = new AccountDAO();
294
+
295
+ Account result = dao.findByLogin(login);
296
+
297
+ if (result != null &&
298
+
299
+ result.getUserId().contentEquals("toumasu") &&
300
+
301
+ result.getPass().contentEquals("1212") &&
302
+
303
+ result.getMail().contentEquals("toumasuo@gmail.com") &&
304
+
305
+ result.getName().contentEquals("トーマス") &&
306
+
307
+ result.getAge() == 19) {
308
+
309
+ System.out.println("findByLogin1:成功しました");
310
+
311
+ } else {
312
+
313
+ System.out.println("findByLogin1:失敗しました");
320
314
 
321
315
  }
322
316
 
323
-
317
+ }
324
-
318
+
319
+
320
+
325
- //メゾットチェックワードを定義
321
+ public static void testFindByLogin2() {
326
-
322
+
327
- boolean checkWord(String userid,String pass){//[20]
323
+ Login login = new Login("toumasu", "12345");
324
+
328
-
325
+ AccountDAO dao = new AccountDAO();
326
+
327
+ Account result = dao.findByLogin(login);
328
+
329
- if (userid == null)
329
+ if (result == null) {
330
+
330
-
331
+ System.out.println("findByLogin2:成功しました");
332
+
331
- return false;
333
+ } else {
332
-
333
- if(pass == null)
334
+
334
-
335
- return false;
336
-
337
-
338
-
339
- if (userid.equals("user01")&&pass.equals("taqusu"))//[23]
335
+ System.out.println("findByLogin2:失敗しました");
340
-
341
- return true;//[24]
342
-
343
-
344
-
345
- return false;//[27]
346
336
 
347
337
  }
348
338
 
349
- }
339
+ }
340
+
350
-
341
+ }
342
+
351
- ```
343
+ ```
344
+
352
-
345
+ ```java
346
+
347
+
348
+
353
-
349
+ ```
350
+
354
-
351
+ ```java
352
+
353
+
354
+
355
-
355
+ ```
356
-
356
+
357
+
358
+
357
- ### 試したこと
359
+ ### 補足情報
358
-
359
-
360
-
360
+
361
- booleanで単純な正誤判定をしsqlで取得した文字列を照合しようとしたがうまくいきませんでした。
361
+ eclipse 
362
-
363
-
364
-
362
+
365
- ### 補足情報(FW/ツールのバージョンなど)
363
+ Version: 2018-12 (4.10.0)ールのバージョンなど)
366
-
364
+
367
- eclipse,postgresql
365
+ PostgreSQL PgAdmin4 ver 3.5
366
+
367
+
368
368
 
369
369
 
370
370