質問編集履歴

4

別エラーの発生のため、本文内容を変更

2018/07/26 02:00

投稿

zvub1123
zvub1123

スコア230

test CHANGED
@@ -1 +1 @@
1
- Lambda(java)でのPostgre接続エラー
1
+ Lambda(java)からRDS(PostgreSQL)にINSERTできない
test CHANGED
@@ -26,144 +26,278 @@
26
26
 
27
27
  --
28
28
 
29
+
30
+
31
+ INSERT 用のメソッドがエラーにならず実行されることは確認できたのですが、以下のように、全てNULLの行が挿入されてしまいます。
32
+
33
+
34
+
35
+ ●テストデータ
36
+
37
+
38
+
39
+ ```json
40
+
41
+ {
42
+
43
+ "name": "Chris",
44
+
45
+ "pass": "XmasParty",
46
+
29
- 以下のエラーが出ています。
47
+ "age": 20
48
+
49
+ }
50
+
51
+ ```
52
+
53
+
54
+
55
+ ●ソース
30
56
 
31
57
  ```java
32
58
 
33
- {
34
-
35
- "errorMessage": "org.postgresql.Driver",
36
-
37
- "errorType": "java.lang.ClassNotFoundException",
38
-
39
- "stackTrace": [
40
-
41
- "java.net.URLClassLoader.findClass(URLClassLoader.java:381)",
42
-
43
- "java.lang.ClassLoader.loadClass(ClassLoader.java:424)",
44
-
45
- "java.lang.ClassLoader.loadClass(ClassLoader.java:357)",
46
-
47
- "java.lang.Class.forName0(Native Method)",
48
-
49
- "java.lang.Class.forName(Class.java:264)",
50
-
51
- "lambda.InsertRecord.connectionTest(InsertRecord.java:20)",
52
-
53
- "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
54
-
55
- "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
56
-
57
- "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
58
-
59
- "java.lang.reflect.Method.invoke(Method.java:498)"
60
-
61
- ]
59
+ public class InsertRecord implements RequestHandler<Request, Response>{
60
+
61
+ // 省略:DB接続部分
62
+
63
+ public Response handleRequest(Request request, Context context) {
64
+
65
+ Response response = new Response();
66
+
67
+ try {
68
+
69
+ System.out.println(request);
70
+
71
+
72
+
73
+ String sql = "INSERT INTO test(name, pass, age) values(" + request.getName() + ", " + request.getPass() + ", " + request.getAge() + ")";
74
+
75
+
76
+
77
+ conn = DriverManager.getConnection(url, user, pass);
78
+
79
+ Statement stmt = conn.createStatement();
80
+
81
+
82
+
83
+ int result = stmt.executeUpdate(sql);
84
+
85
+
86
+
87
+ ResultSet resultSet = stmt.executeQuery("SELECT * FROM test WHERE name = " + request.getName());
88
+
89
+
90
+
91
+ // フィールド一覧を取得
92
+
93
+ List<String> fields = new ArrayList<String>();
94
+
95
+ ResultSetMetaData rsmd = resultSet.getMetaData();
96
+
97
+ for (int i = 1; i <= rsmd.getColumnCount(); i++) {
98
+
99
+ fields.add(rsmd.getColumnName(i));
100
+
101
+ }
102
+
103
+
104
+
105
+ //結果の出力
106
+
107
+ int rowCount = 0;
108
+
109
+ while (resultSet.next()) {
110
+
111
+ rowCount++;
112
+
113
+
114
+
115
+ System.out.println("---------------------------------------------------");
116
+
117
+ System.out.println("--- Rows:" + rowCount);
118
+
119
+ System.out.println("---------------------------------------------------");
120
+
121
+
122
+
123
+ //値は、「resultSet.getString(<フィールド名>)」で取得する。
124
+
125
+ for (String field : fields) {
126
+
127
+ response.setFields(field, resultSet.getString(field));
128
+
129
+ }
130
+
131
+ }
132
+
133
+
134
+
135
+ stmt.close();
136
+
137
+ conn.close();
138
+
139
+
140
+
141
+ } catch(SQLException e) {
142
+
143
+ e.printStackTrace();
144
+
145
+ }
146
+
147
+ return response;
148
+
149
+ }
62
150
 
63
151
  }
64
152
 
153
+
154
+
65
155
  ```
66
156
 
67
157
 
68
158
 
159
+ 上記のコードは各種Webページを参考にさせていただきながら記述したのですが、
160
+
161
+ 結果は次のようなエラーとなります。
162
+
163
+
164
+
165
+ ```Java
166
+
167
+ START RequestId: 59f2199d-9072-11e8-8a69-5b2ce4631401 Version: $LATEST
168
+
169
+ lambda.Request@1f28c152
170
+
171
+ org.postgresql.util.PSQLException: ERROR: column "chris" does not exist
172
+
173
+ Position: 42
174
+
175
+ at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
176
+
177
+ at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
178
+
179
+ at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
180
+
181
+ at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
182
+
183
+ at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
184
+
185
+ at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:302)
186
+
187
+ at lambda.InsertRecord.handleRequest(InsertRecord.java:66)
188
+
189
+ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
190
+
191
+ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
192
+
193
+ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
194
+
195
+ at java.lang.reflect.Method.invoke(Method.java:498)
196
+
197
+ at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:259)
198
+
199
+ at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:178)
200
+
201
+ at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
202
+
203
+ at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:283)
204
+
205
+ at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
206
+
207
+ at java.lang.Class.forName0(Native Method)
208
+
209
+ at java.lang.Class.forName(Class.java:348)
210
+
211
+ at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:94)
212
+
213
+ END RequestId: 59f2199d-9072-11e8-8a69-5b2ce4631401
214
+
215
+ REPORT RequestId: 59f2199d-9072-11e8-8a69-5b2ce4631401 Duration: 864.50 ms Billed Duration: 900 ms Memory Size: 512 MB Max Memory Used: 49 MB
216
+
217
+ ```
218
+
219
+
220
+
69
- イバREAD部分に問題があるようなのですが、どなたかご教示いただけると助かります。
221
+ 上記のエ解決方法をご教示いただけますでしょうか
222
+
223
+ また、オリジナルのPOJOクラスは以下となりますが、InsertRecordクラスの中に含まなければならない、などの制約はあるのでしょうか。
70
224
 
71
225
 
72
226
 
73
227
  ```java
74
228
 
229
+ public class Request {
230
+
231
+ String name;
232
+
233
+ String pass;
234
+
235
+ int age;
236
+
237
+
238
+
239
+ public String getName() {
240
+
241
+ return name;
242
+
243
+ }
244
+
245
+ public String getPass() {
246
+
247
+ return pass;
248
+
249
+ }
250
+
251
+ public String getAge() {
252
+
253
+ return String.valueOf(age);
254
+
255
+ }
256
+
257
+
258
+
259
+ public void setName(String name) {
260
+
261
+ this.name = name;
262
+
263
+ }
264
+
265
+ public void setPass(String pass) {
266
+
267
+ this.pass = pass;
268
+
269
+ }
270
+
271
+ public void setAge(String age) {
272
+
273
+ this.age = Integer.parseInt(age);
274
+
275
+ }
276
+
277
+
278
+
75
- public void connectionTest(Request request) throws ClassNotFoundException {
279
+ public Request(String name, String pass, String age) {
76
-
280
+
77
- Connection conn = null;
281
+ this.name = name;
78
-
79
- String url = "jdbc:postgresql://<DB Endpoint>:<PORT>/<DB Name>";
282
+
80
-
81
- String user = "<user name>";
283
+ this.pass = pass;
82
-
284
+
83
- String pass = "<password>";
285
+ this.age = Integer.parseInt(age);
84
-
85
-
86
-
286
+
87
- try {
287
+ }
88
-
89
- Class.forName("org.postgresql.Driver");
288
+
90
-
91
-
92
-
93
- conn = DriverManager.getConnection(url, user, pass);
94
-
95
- Statement stmt = conn.createStatement();
289
+ public Request() {
96
-
97
- ResultSet resultSet = stmt.executeQuery("SELECT * FROM test");
290
+
98
-
99
- ...
291
+ }
292
+
293
+ }
294
+
295
+
100
296
 
101
297
  ```
102
298
 
103
299
 
104
300
 
105
- [パッケージ・エクスプローラー]→[ビルド・パス]→[外部アーカイブの追加]からPostgreのjarも追加済みです。
301
+ 以上です。
106
302
 
107
303
  よろしくお願いします。
108
-
109
-
110
-
111
- 追記
112
-
113
- --
114
-
115
-
116
-
117
- JDBC 4.0 からは
118
-
119
-
120
-
121
- ```java
122
-
123
- Class.forName();
124
-
125
- ```
126
-
127
-
128
-
129
- の記述が不要となっていたことが分かったので、該当行をコメントアウトしたところ、
130
-
131
- エラーが以下のように変わりました。
132
-
133
-
134
-
135
- ```java
136
-
137
- java.sql.SQLException: No suitable driver found for jdbc:postgresql://<DB Endpoint>:<PORT>/<DB Name>
138
-
139
- at java.sql.DriverManager.getConnection(DriverManager.java:689)
140
-
141
- at java.sql.DriverManager.getConnection(DriverManager.java:247)
142
-
143
- at lambda.InsertRecord.connectionTest(InsertRecord.java:22)
144
-
145
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
146
-
147
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
148
-
149
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
150
-
151
- at java.lang.reflect.Method.invoke(Method.java:498)
152
-
153
- at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:259)
154
-
155
- at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:178)
156
-
157
- at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
158
-
159
- at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:286)
160
-
161
- at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
162
-
163
- at java.lang.Class.forName0(Native Method)
164
-
165
- at java.lang.Class.forName(Class.java:348)
166
-
167
- at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:94)
168
-
169
- ```

3

Postgreのバージョン修正、SRE ver の修正

2018/07/26 02:00

投稿

zvub1123
zvub1123

スコア230

test CHANGED
File without changes
test CHANGED
@@ -8,9 +8,9 @@
8
8
 
9
9
  eclipse version    :Oxygen.3a Release (4.7.3a)
10
10
 
11
- PostgreSQL      :42.2.4.jre6.jar
11
+ PostgreSQL      :42.2.4.jar
12
12
 
13
- Java      :J2SE-1.5
13
+ SRE System Library  JavaSE-1.8
14
14
 
15
15
 
16
16
 

2

エラー内容の追記

2018/07/25 02:10

投稿

zvub1123
zvub1123

スコア230

test CHANGED
File without changes
test CHANGED
@@ -105,3 +105,65 @@
105
105
  [パッケージ・エクスプローラー]→[ビルド・パス]→[外部アーカイブの追加]からPostgreのjarも追加済みです。
106
106
 
107
107
  よろしくお願いします。
108
+
109
+
110
+
111
+ 追記
112
+
113
+ --
114
+
115
+
116
+
117
+ JDBC 4.0 からは
118
+
119
+
120
+
121
+ ```java
122
+
123
+ Class.forName();
124
+
125
+ ```
126
+
127
+
128
+
129
+ の記述が不要となっていたことが分かったので、該当行をコメントアウトしたところ、
130
+
131
+ エラーが以下のように変わりました。
132
+
133
+
134
+
135
+ ```java
136
+
137
+ java.sql.SQLException: No suitable driver found for jdbc:postgresql://<DB Endpoint>:<PORT>/<DB Name>
138
+
139
+ at java.sql.DriverManager.getConnection(DriverManager.java:689)
140
+
141
+ at java.sql.DriverManager.getConnection(DriverManager.java:247)
142
+
143
+ at lambda.InsertRecord.connectionTest(InsertRecord.java:22)
144
+
145
+ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
146
+
147
+ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
148
+
149
+ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
150
+
151
+ at java.lang.reflect.Method.invoke(Method.java:498)
152
+
153
+ at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:259)
154
+
155
+ at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:178)
156
+
157
+ at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
158
+
159
+ at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:286)
160
+
161
+ at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
162
+
163
+ at java.lang.Class.forName0(Native Method)
164
+
165
+ at java.lang.Class.forName(Class.java:348)
166
+
167
+ at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:94)
168
+
169
+ ```

1

Postgreのバージョン修正、url の修正

2018/07/25 01:31

投稿

zvub1123
zvub1123

スコア230

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  eclipse version    :Oxygen.3a Release (4.7.3a)
10
10
 
11
- PostgreSQL      :42.2.4.jar
11
+ PostgreSQL      :42.2.4.jre6.jar
12
12
 
13
13
  Java      :J2SE-1.5
14
14
 
@@ -76,7 +76,7 @@
76
76
 
77
77
  Connection conn = null;
78
78
 
79
- String url = "jdbc:postgre://<DB Endpoint>:<PORT>/<DB Name>";
79
+ String url = "jdbc:postgresql://<DB Endpoint>:<PORT>/<DB Name>";
80
80
 
81
81
  String user = "<user name>";
82
82