質問編集履歴
3
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -349,3 +349,243 @@
|
|
349
349
|
ヒントをもらえると嬉しいです。
|
350
350
|
|
351
351
|
また、再回答のほどよろしくお願いします。
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
```ここに言語を入力
|
356
|
+
|
357
|
+
import java.sql.Connection;
|
358
|
+
|
359
|
+
import java.sql.DriverManager;
|
360
|
+
|
361
|
+
import java.sql.Statement;
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
public class input {
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
private static String sqlStr1;
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
public static void input() {
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
String msg = "";
|
394
|
+
|
395
|
+
try {
|
396
|
+
|
397
|
+
// ドライバロード
|
398
|
+
|
399
|
+
Class.forName("com.mysql.jdbc.Driver");
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
System.out.println("データの入力をしてください。");
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
System.out.println("id");
|
424
|
+
|
425
|
+
int user_id =100; //= new Scanner(System.in).nextLine();
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
System.out.println("name");
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
String user_name ="test"; //= new Scanner(System.in).nextLine();
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
// MySQL に接続
|
442
|
+
|
443
|
+
Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.8:3306/testdb", "test11", "");
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
// ステートメント生成
|
448
|
+
|
449
|
+
Statement stmt = con.createStatement();
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
//SQL を実行
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
String insert = "insert into test_table(user_id,user_name) values (" + user_id + "," + user_name + ")";
|
464
|
+
|
465
|
+
//String sqlStr = "insert into test_table(user_id,user_name) values(2,'satou');";
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
int num = stmt.executeUpdate(insert);
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
output a =new output();
|
476
|
+
|
477
|
+
a.output();
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
// 接続を閉じる
|
484
|
+
|
485
|
+
stmt.close();
|
486
|
+
|
487
|
+
con.close();
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
}catch (Exception e){
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
e.printStackTrace() ;
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
}
|
514
|
+
|
515
|
+
}
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
|
526
|
+
|
527
|
+
```
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
さらにコードを変更しました。
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
```ここに言語を入力
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'test' in 'field list'
|
540
|
+
|
541
|
+
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
|
542
|
+
|
543
|
+
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
|
544
|
+
|
545
|
+
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
|
546
|
+
|
547
|
+
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
|
548
|
+
|
549
|
+
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
|
550
|
+
|
551
|
+
at com.mysql.jdbc.Util.getInstance(Util.java:408)
|
552
|
+
|
553
|
+
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
|
554
|
+
|
555
|
+
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
|
556
|
+
|
557
|
+
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
|
558
|
+
|
559
|
+
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
|
560
|
+
|
561
|
+
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
|
562
|
+
|
563
|
+
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2483)
|
564
|
+
|
565
|
+
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552)
|
566
|
+
|
567
|
+
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607)
|
568
|
+
|
569
|
+
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480)
|
570
|
+
|
571
|
+
at input.input(input.java:57)
|
572
|
+
|
573
|
+
```
|
574
|
+
|
575
|
+
このようなエラーが出力されました。
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
```ここに言語を入力
|
580
|
+
|
581
|
+
int num = stmt.executeUpdate(insert);
|
582
|
+
|
583
|
+
```
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
やはりこのコードではないかと思っています。
|
588
|
+
|
589
|
+
|
590
|
+
|
591
|
+
回答よろしくお願いいたします。
|
2
コードの変更点
test
CHANGED
File without changes
|
test
CHANGED
@@ -137,3 +137,215 @@
|
|
137
137
|
|
138
138
|
|
139
139
|
どうコードを変更すればよいでしょうか
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
変更コード
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
```ここに言語を入力
|
148
|
+
|
149
|
+
import java.sql.Connection;
|
150
|
+
|
151
|
+
import java.sql.DriverManager;
|
152
|
+
|
153
|
+
import java.sql.Statement;
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
public class input {
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
private static String sqlStr1;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
public static void input() {
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
String msg = "";
|
186
|
+
|
187
|
+
try {
|
188
|
+
|
189
|
+
// ドライバロード
|
190
|
+
|
191
|
+
Class.forName("com.mysql.jdbc.Driver");
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
System.out.println("データの入力をしてください。");
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
System.out.println("id");
|
216
|
+
|
217
|
+
int user_id =100; //= new Scanner(System.in).nextLine();
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
System.out.println("name");
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
int user_name =1; //= new Scanner(System.in).nextLine();
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
// MySQL に接続
|
234
|
+
|
235
|
+
Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.8:3306/testdb", "test11", "");
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
// ステートメント生成
|
240
|
+
|
241
|
+
Statement stmt = con.createStatement();
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
//SQL を実行
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
String insert = "insert into test_table(user_id,user_name) values (" + user_id + "," + user_name + ")";
|
256
|
+
|
257
|
+
//String sqlStr = "insert into test_table(user_id,user_name) values(2,'satou');";
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
int num = stmt.executeUpdate(insert);
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
output a =new output();
|
268
|
+
|
269
|
+
a.output();
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
// 接続を閉じる
|
276
|
+
|
277
|
+
stmt.close();
|
278
|
+
|
279
|
+
con.close();
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
}catch (Exception e){
|
288
|
+
|
289
|
+
msg = "ドライバのロードに失敗しました";
|
290
|
+
|
291
|
+
System.out.println(msg);
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
```
|
308
|
+
|
309
|
+
```ここに言語を入力
|
310
|
+
|
311
|
+
int user_name =1;
|
312
|
+
|
313
|
+
```
|
314
|
+
|
315
|
+
を
|
316
|
+
|
317
|
+
```ここに言語を入力
|
318
|
+
|
319
|
+
String user_name = "1";
|
320
|
+
|
321
|
+
```
|
322
|
+
|
323
|
+
にするとエラーが出る。
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
```ここに言語を入力
|
328
|
+
|
329
|
+
int num = stmt.executeUpdate(insert);
|
330
|
+
|
331
|
+
```
|
332
|
+
|
333
|
+
ここが問題あるのではないかと思うがここを
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
```ここに言語を入力
|
338
|
+
|
339
|
+
String num = stmt.executeUpdate(insert);
|
340
|
+
|
341
|
+
```
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
にすとエラーが出る。
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
ヒントをもらえると嬉しいです。
|
350
|
+
|
351
|
+
また、再回答のほどよろしくお願いします。
|
1
校正
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|