回答編集履歴
7
追加
test
CHANGED
@@ -147,3 +147,27 @@
|
|
147
147
|
}
|
148
148
|
}
|
149
149
|
```
|
150
|
+
```plain
|
151
|
+
----------------
|
152
|
+
parameter: id='', password=''
|
153
|
+
sql=UPDATE employee_table2 SET login_time='2022/10/18 20:09:01' WHERE id=''
|
154
|
+
→ null
|
155
|
+
----------------
|
156
|
+
parameter: id='foobar', password=''
|
157
|
+
sql=UPDATE employee_table2 SET login_time='2022/10/18 20:09:01' WHERE id='foobar'
|
158
|
+
sql=SELECT name,comment FROM employee_table2 WHERE id='foobar' AND password=''
|
159
|
+
→ null
|
160
|
+
----------------
|
161
|
+
parameter: id='', password='qwerty'
|
162
|
+
sql=UPDATE employee_table2 SET login_time='2022/10/18 20:09:01' WHERE id=''
|
163
|
+
→ null
|
164
|
+
----------------
|
165
|
+
parameter: id='foobar', password='qwerty'
|
166
|
+
sql=UPDATE employee_table2 SET login_time='2022/10/18 20:09:01' WHERE id='foobar'
|
167
|
+
sql=SELECT name,comment FROM employee_table2 WHERE id='foobar' AND password='qwerty'
|
168
|
+
→ id=foobar,password=qwerty,name=USER,comment=ABCDEFGHIJKLMNOPQRSTUVWXYZ,loginTime=2022/10/18 20:09:01
|
169
|
+
----------------
|
170
|
+
parameter: id='123', password='ABC'
|
171
|
+
sql=UPDATE employee_table2 SET login_time='2022/10/18 20:09:01' WHERE id='123'
|
172
|
+
→ null
|
173
|
+
```
|
6
コード修正
test
CHANGED
@@ -26,8 +26,6 @@
|
|
26
26
|
---
|
27
27
|
サーブレットから必要なパラメータが取得出来ていることを確認出来れば、後はデータベース関連だけですので、以下のようなテストプログラムを作って SQL 文やコードが動くことを確認しながら本来のコードを修正することが出来ると思います。
|
28
28
|
```java
|
29
|
-
package teratail_java.q_d9xi0zz1htl8a7;
|
30
|
-
|
31
29
|
import java.sql.*;
|
32
30
|
import java.text.SimpleDateFormat;
|
33
31
|
import java.util.StringJoiner;
|
5
追加
test
CHANGED
@@ -23,3 +23,129 @@
|
|
23
23
|
```plain
|
24
24
|
sql=SELECT * FROM table WHERE id=123 AND name='ABC'
|
25
25
|
```
|
26
|
+
---
|
27
|
+
サーブレットから必要なパラメータが取得出来ていることを確認出来れば、後はデータベース関連だけですので、以下のようなテストプログラムを作って SQL 文やコードが動くことを確認しながら本来のコードを修正することが出来ると思います。
|
28
|
+
```java
|
29
|
+
package teratail_java.q_d9xi0zz1htl8a7;
|
30
|
+
|
31
|
+
import java.sql.*;
|
32
|
+
import java.text.SimpleDateFormat;
|
33
|
+
import java.util.StringJoiner;
|
34
|
+
|
35
|
+
public class PostgreSQLTest {
|
36
|
+
// set/get で長くなるので代用
|
37
|
+
static class EmployeeBean {
|
38
|
+
String id;
|
39
|
+
String password;
|
40
|
+
String name;
|
41
|
+
String comment;
|
42
|
+
String loginTime;
|
43
|
+
|
44
|
+
@Override
|
45
|
+
public String toString() {
|
46
|
+
StringJoiner sj = new StringJoiner(",");
|
47
|
+
sj.add("id=" + id);
|
48
|
+
sj.add("password=" + password);
|
49
|
+
sj.add("name=" + name);
|
50
|
+
sj.add("comment=" + comment);
|
51
|
+
sj.add("loginTime=" + loginTime);
|
52
|
+
return sj.toString();
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
static final String TABLE = "employee_table2";
|
57
|
+
|
58
|
+
static SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
59
|
+
|
60
|
+
public static void main(String[] args) throws SQLException {
|
61
|
+
initDB();
|
62
|
+
|
63
|
+
test("", "");
|
64
|
+
test("foobar", "");
|
65
|
+
test("", "qwerty");
|
66
|
+
test("foobar", "qwerty");
|
67
|
+
test("123", "ABC");
|
68
|
+
}
|
69
|
+
|
70
|
+
private static void test(String id, String password) throws SQLException {
|
71
|
+
System.out.println("----------------");
|
72
|
+
System.out.println("parameter: id='" + id + "', password='" + password + "'");
|
73
|
+
EmployeeBean bean = search(id, password);
|
74
|
+
System.out.println(" → " + bean);
|
75
|
+
}
|
76
|
+
|
77
|
+
private static EmployeeBean search(String id, String password) throws SQLException {
|
78
|
+
String loginTime = sdFormat.format(new java.util.Date());
|
79
|
+
|
80
|
+
try(Connection con = createConnection()) {
|
81
|
+
try(PreparedStatement pstmt = con.prepareStatement(
|
82
|
+
"UPDATE " + TABLE +
|
83
|
+
" SET login_time=?" +
|
84
|
+
" WHERE id=?")) {
|
85
|
+
pstmt.setString(1, loginTime);
|
86
|
+
pstmt.setString(2, id);
|
87
|
+
System.out.println("sql=" + pstmt.toString());
|
88
|
+
if(pstmt.executeUpdate() != 1) return null; //id が無い
|
89
|
+
}
|
90
|
+
try(PreparedStatement pstmt = con.prepareStatement(
|
91
|
+
"SELECT" +
|
92
|
+
" name,comment" +
|
93
|
+
" FROM " + TABLE +
|
94
|
+
" WHERE id=? AND password=?")) {
|
95
|
+
pstmt.setString(1, id);
|
96
|
+
pstmt.setString(2, password);
|
97
|
+
System.out.println("sql=" + pstmt.toString());
|
98
|
+
try(ResultSet rset = pstmt.executeQuery()) {
|
99
|
+
if(rset.next()) {
|
100
|
+
EmployeeBean bean = new EmployeeBean();
|
101
|
+
bean.id = id;
|
102
|
+
bean.password = password;
|
103
|
+
bean.name = rset.getString("name");
|
104
|
+
bean.comment = rset.getString("comment");
|
105
|
+
bean.loginTime = loginTime;
|
106
|
+
return bean;
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
111
|
+
return null;
|
112
|
+
}
|
113
|
+
|
114
|
+
//テーブル・データ準備
|
115
|
+
private static void initDB() throws SQLException {
|
116
|
+
try(Connection con = createConnection()) {
|
117
|
+
con.setAutoCommit(false);
|
118
|
+
try(Statement stmt = con.createStatement()) {
|
119
|
+
stmt.execute("DROP TABLE IF EXISTS " + TABLE);
|
120
|
+
stmt.execute(
|
121
|
+
"CREATE TABLE " + TABLE + " (" +
|
122
|
+
" id VARCHAR(32) PRIMARY KEY," +
|
123
|
+
" password VARCHAR(32)," +
|
124
|
+
" name VARCHAR(64)," +
|
125
|
+
" comment TEXT," +
|
126
|
+
" login_time VARCHAR(20)" +
|
127
|
+
" )");
|
128
|
+
}
|
129
|
+
try(PreparedStatement pstmt = con.prepareStatement(
|
130
|
+
"INSERT INTO" +
|
131
|
+
" " + TABLE + " (id,password,name,comment,login_time)" +
|
132
|
+
" VALUES (?,?,?,?,?)")) {
|
133
|
+
pstmt.setString(1, "foobar");
|
134
|
+
pstmt.setString(2, "qwerty");
|
135
|
+
pstmt.setString(3, "USER");
|
136
|
+
pstmt.setString(4, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
137
|
+
pstmt.setString(5, sdFormat.format(new java.util.Date()));
|
138
|
+
if(pstmt.executeUpdate() != 1) {
|
139
|
+
System.err.println("insert 失敗");
|
140
|
+
return; //rollback
|
141
|
+
}
|
142
|
+
}
|
143
|
+
con.commit();
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
private static Connection createConnection() throws SQLException {
|
148
|
+
return DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "password");
|
149
|
+
}
|
150
|
+
}
|
151
|
+
```
|
4
脱字
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
間違いは実行する所だけとは限りません。
|
3
3
|
|
4
4
|
---
|
5
|
-
以下のコードで PrepareStatement が実際にどのような SQL を生成しているか確認出来ることを確認しました。
|
5
|
+
以下のコードで PreparedStatement が実際にどのような SQL を生成しているか確認出来ることを確認しました。
|
6
6
|
JDBC4 からは ```Class.forName(~)``` は不要になりました。
|
7
7
|
(postgresql-42.5.0.jar 使用)
|
8
8
|
```java
|
3
修正
test
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
|
4
4
|
---
|
5
5
|
以下のコードで PrepareStatement が実際にどのような SQL を生成しているか確認出来ることを確認しました。
|
6
|
+
JDBC4 からは ```Class.forName(~)``` は不要になりました。
|
6
7
|
(postgresql-42.5.0.jar 使用)
|
7
8
|
```java
|
8
9
|
import java.sql.*;
|
2
追加
test
CHANGED
@@ -1,2 +1,24 @@
|
|
1
1
|
例外のメッセージとコードを隅から隅まで確認してください。
|
2
2
|
間違いは実行する所だけとは限りません。
|
3
|
+
|
4
|
+
---
|
5
|
+
以下のコードで PrepareStatement が実際にどのような SQL を生成しているか確認出来ることを確認しました。
|
6
|
+
(postgresql-42.5.0.jar 使用)
|
7
|
+
```java
|
8
|
+
import java.sql.*;
|
9
|
+
|
10
|
+
public class PostgreSQLTest {
|
11
|
+
public static void main(String[] args) throws SQLException {
|
12
|
+
try(Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "password")) {
|
13
|
+
try(PreparedStatement pstmt = con.prepareStatement("SELECT * FROM table WHERE id=? AND name=?");) {
|
14
|
+
pstmt.setLong(1, 123);
|
15
|
+
pstmt.setString(2, "ABC");
|
16
|
+
System.out.println("sql=" + pstmt.toString());
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}
|
20
|
+
}
|
21
|
+
```
|
22
|
+
```plain
|
23
|
+
sql=SELECT * FROM table WHERE id=123 AND name='ABC'
|
24
|
+
```
|
1
追加
test
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
コードを隅から隅まで確認してください。
|
1
|
+
例外のメッセージとコードを隅から隅まで確認してください。
|
2
2
|
間違いは実行する所だけとは限りません。
|