teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

コード追加

2017/11/20 11:12

投稿

riroholll
riroholll

スコア57

title CHANGED
File without changes
body CHANGED
@@ -173,4 +173,124 @@
173
173
  にすとエラーが出る。
174
174
 
175
175
  ヒントをもらえると嬉しいです。
176
- また、再回答のほどよろしくお願いします。
176
+ また、再回答のほどよろしくお願いします。
177
+
178
+ ```ここに言語を入力
179
+ import java.sql.Connection;
180
+ import java.sql.DriverManager;
181
+ import java.sql.Statement;
182
+
183
+
184
+
185
+ public class input {
186
+
187
+ private static String sqlStr1;
188
+
189
+
190
+
191
+ public static void input() {
192
+
193
+
194
+
195
+
196
+
197
+ String msg = "";
198
+ try {
199
+ // ドライバロード
200
+ Class.forName("com.mysql.jdbc.Driver");
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+ System.out.println("データの入力をしてください。");
211
+
212
+ System.out.println("id");
213
+ int user_id =100; //= new Scanner(System.in).nextLine();
214
+
215
+ System.out.println("name");
216
+
217
+
218
+ String user_name ="test"; //= new Scanner(System.in).nextLine();
219
+
220
+
221
+ // MySQL に接続
222
+ Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.8:3306/testdb", "test11", "");
223
+
224
+ // ステートメント生成
225
+ Statement stmt = con.createStatement();
226
+
227
+
228
+
229
+
230
+ //SQL を実行
231
+
232
+ String insert = "insert into test_table(user_id,user_name) values (" + user_id + "," + user_name + ")";
233
+ //String sqlStr = "insert into test_table(user_id,user_name) values(2,'satou');";
234
+
235
+ int num = stmt.executeUpdate(insert);
236
+
237
+
238
+ output a =new output();
239
+ a.output();
240
+
241
+
242
+ // 接続を閉じる
243
+ stmt.close();
244
+ con.close();
245
+
246
+
247
+
248
+ }catch (Exception e){
249
+
250
+ e.printStackTrace() ;
251
+
252
+ }
253
+
254
+
255
+
256
+
257
+ }
258
+ }
259
+
260
+
261
+
262
+
263
+
264
+ ```
265
+
266
+ さらにコードを変更しました。
267
+
268
+ ```ここに言語を入力
269
+
270
+ com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'test' in 'field list'
271
+ at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
272
+ at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
273
+ at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
274
+ at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
275
+ at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
276
+ at com.mysql.jdbc.Util.getInstance(Util.java:408)
277
+ at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
278
+ at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
279
+ at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
280
+ at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
281
+ at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
282
+ at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2483)
283
+ at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552)
284
+ at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2607)
285
+ at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1480)
286
+ at input.input(input.java:57)
287
+ ```
288
+ このようなエラーが出力されました。
289
+
290
+ ```ここに言語を入力
291
+ int num = stmt.executeUpdate(insert);
292
+ ```
293
+
294
+ やはりこのコードではないかと思っています。
295
+
296
+ 回答よろしくお願いいたします。

2

コードの変更点

2017/11/20 11:12

投稿

riroholll
riroholll

スコア57

title CHANGED
File without changes
body CHANGED
@@ -67,4 +67,110 @@
67
67
  ドライバのロードに失敗しました
68
68
  ```
69
69
 
70
- どうコードを変更すればよいでしょうか
70
+ どうコードを変更すればよいでしょうか
71
+
72
+ 変更コード
73
+
74
+ ```ここに言語を入力
75
+ import java.sql.Connection;
76
+ import java.sql.DriverManager;
77
+ import java.sql.Statement;
78
+
79
+
80
+
81
+ public class input {
82
+
83
+ private static String sqlStr1;
84
+
85
+
86
+
87
+ public static void input() {
88
+
89
+
90
+
91
+
92
+
93
+ String msg = "";
94
+ try {
95
+ // ドライバロード
96
+ Class.forName("com.mysql.jdbc.Driver");
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+ System.out.println("データの入力をしてください。");
107
+
108
+ System.out.println("id");
109
+ int user_id =100; //= new Scanner(System.in).nextLine();
110
+
111
+ System.out.println("name");
112
+
113
+
114
+ int user_name =1; //= new Scanner(System.in).nextLine();
115
+
116
+
117
+ // MySQL に接続
118
+ Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.8:3306/testdb", "test11", "");
119
+
120
+ // ステートメント生成
121
+ Statement stmt = con.createStatement();
122
+
123
+
124
+
125
+
126
+ //SQL を実行
127
+
128
+ String insert = "insert into test_table(user_id,user_name) values (" + user_id + "," + user_name + ")";
129
+ //String sqlStr = "insert into test_table(user_id,user_name) values(2,'satou');";
130
+
131
+ int num = stmt.executeUpdate(insert);
132
+
133
+
134
+ output a =new output();
135
+ a.output();
136
+
137
+
138
+ // 接続を閉じる
139
+ stmt.close();
140
+ con.close();
141
+
142
+
143
+
144
+ }catch (Exception e){
145
+ msg = "ドライバのロードに失敗しました";
146
+ System.out.println(msg);
147
+ }
148
+
149
+
150
+
151
+
152
+ }
153
+ }
154
+ ```
155
+ ```ここに言語を入力
156
+ int user_name =1;
157
+ ```
158
+
159
+ ```ここに言語を入力
160
+ String user_name = "1";
161
+ ```
162
+ にするとエラーが出る。
163
+
164
+ ```ここに言語を入力
165
+ int num = stmt.executeUpdate(insert);
166
+ ```
167
+ ここが問題あるのではないかと思うがここを
168
+
169
+ ```ここに言語を入力
170
+ String num = stmt.executeUpdate(insert);
171
+ ```
172
+
173
+ にすとエラーが出る。
174
+
175
+ ヒントをもらえると嬉しいです。
176
+ また、再回答のほどよろしくお願いします。

1

校正

2017/11/19 10:52

投稿

riroholll
riroholll

スコア57

title CHANGED
File without changes
body CHANGED
File without changes