質問編集履歴
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -193,195 +193,3 @@
|
|
193
193
|
2:条件検索
|
194
194
|
|
195
195
|
![where句のみ](0166d78a2df673a851a0052a45dd5f70.gif)
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
###追記
|
200
|
-
|
201
|
-
以下のコードに修正して、しっかりとエスケープ処理をして文字列として扱えるか検証してみましたが上手く行きません。
|
202
|
-
|
203
|
-
何故かわからないです。ご教示頂ければ幸いです。
|
204
|
-
|
205
|
-
```java
|
206
|
-
|
207
|
-
static BufferedReader _br = new BufferedReader(new InputStreamReader(System.in));
|
208
|
-
|
209
|
-
// final String SQL = "Select EMPID, ENAME, JOB from EMPLOYEES ";
|
210
|
-
|
211
|
-
public static void main(String[] args) {
|
212
|
-
|
213
|
-
System.out.println("検索したい文字をを入力して下さい。");
|
214
|
-
|
215
|
-
System.out.println("入力する内容は正しいSQL文、かつ、選択したテーブルに則した内容でなければならない。");
|
216
|
-
|
217
|
-
System.out.println("Select EMPID, ENAME, JOB from EMPLOYEES where JOB like");
|
218
|
-
|
219
|
-
System.out.println("");
|
220
|
-
|
221
|
-
DDD d = new DDD();
|
222
|
-
|
223
|
-
boolean flg = true;
|
224
|
-
|
225
|
-
String word = null;
|
226
|
-
|
227
|
-
while (flg) {
|
228
|
-
|
229
|
-
try {
|
230
|
-
|
231
|
-
// input where clause
|
232
|
-
|
233
|
-
word = _br.readLine();
|
234
|
-
|
235
|
-
d.getName(word);
|
236
|
-
|
237
|
-
flg = false;
|
238
|
-
|
239
|
-
} catch (IOException e) {
|
240
|
-
|
241
|
-
System.out.println("入力された値は不正です。");
|
242
|
-
|
243
|
-
continue;
|
244
|
-
|
245
|
-
}
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
private void getName(String word){
|
252
|
-
|
253
|
-
Connection con = null;
|
254
|
-
|
255
|
-
String sql = null;
|
256
|
-
|
257
|
-
PreparedStatement ps = null;
|
258
|
-
|
259
|
-
ResultSet rs = null;
|
260
|
-
|
261
|
-
String hostname = "*****";
|
262
|
-
|
263
|
-
String username = "****";
|
264
|
-
|
265
|
-
String pw = "******";
|
266
|
-
|
267
|
-
String sid = "XE";
|
268
|
-
|
269
|
-
String url = "jdbc:oracle:thin:@";
|
270
|
-
|
271
|
-
try{
|
272
|
-
|
273
|
-
Class.forName("oracle.jdbc.driver.OracleDriver");
|
274
|
-
|
275
|
-
con = DriverManager.getConnection(url + hostname + ":1521:" + sid, username, pw);
|
276
|
-
|
277
|
-
sql = "Select EMPID, ENAME, JOB from EMPLOYEES where JOB like?";
|
278
|
-
|
279
|
-
ps = con.prepareStatement(sql);
|
280
|
-
|
281
|
-
ps.setString(1, "%" +this.escape(word) + "%");
|
282
|
-
|
283
|
-
rs = ps.executeQuery();
|
284
|
-
|
285
|
-
while (rs.next()) {
|
286
|
-
|
287
|
-
// System.out.println(rs.getInt("EMPID")+rs.getString("ENAME")+rs.getString("JOB"));
|
288
|
-
|
289
|
-
System.out.println("EMPID " + rs.getInt("EMPID") + " ENAME " + rs.getString("ENAME") + " JOB "
|
290
|
-
|
291
|
-
+ rs.getString("JOB"));
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
}
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
rs.close();
|
302
|
-
|
303
|
-
ps.close();
|
304
|
-
|
305
|
-
} catch (ClassNotFoundException e) {
|
306
|
-
|
307
|
-
System.out.println("There is no JDBC driver");
|
308
|
-
|
309
|
-
} catch (SQLException e) {
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
e.printStackTrace();
|
314
|
-
|
315
|
-
}finally{
|
316
|
-
|
317
|
-
if(con != null){
|
318
|
-
|
319
|
-
try {
|
320
|
-
|
321
|
-
con.close();
|
322
|
-
|
323
|
-
} catch (SQLException e) {
|
324
|
-
|
325
|
-
System.out.println( e.getErrorCode() );
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
}
|
330
|
-
|
331
|
-
}
|
332
|
-
|
333
|
-
}
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
public String escape(String before){
|
340
|
-
|
341
|
-
StringBuilder after = new StringBuilder();
|
342
|
-
|
343
|
-
String esSymbol = "\";
|
344
|
-
|
345
|
-
char es1 = '_';
|
346
|
-
|
347
|
-
char es2 = '%';
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
for (int i = 0; i < before.length(); i++) {
|
352
|
-
|
353
|
-
if (before.charAt(i) == es1 || before.charAt(i) == es2) {
|
354
|
-
|
355
|
-
after.append(esSymbol);
|
356
|
-
|
357
|
-
after.append(String.valueOf(before.charAt(i)));
|
358
|
-
|
359
|
-
continue;
|
360
|
-
|
361
|
-
}
|
362
|
-
|
363
|
-
after.append(String.valueOf(before.charAt(i)));
|
364
|
-
|
365
|
-
}
|
366
|
-
|
367
|
-
return after.toString();
|
368
|
-
|
369
|
-
}
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
}
|
374
|
-
|
375
|
-
```
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
###期待結果:
|
380
|
-
|
381
|
-
入力した値が正しいかわからないのですが、
|
382
|
-
|
383
|
-
以下の値を入力して検索をかけて
|
384
|
-
|
385
|
-
%事務\_チームA%
|
386
|
-
|
387
|
-
![エスケープ処理](bfe03d4dc8a3f7327c9b359e366a151f.gif)
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -193,3 +193,195 @@
|
|
193
193
|
2:条件検索
|
194
194
|
|
195
195
|
![where句のみ](0166d78a2df673a851a0052a45dd5f70.gif)
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
###追記
|
200
|
+
|
201
|
+
以下のコードに修正して、しっかりとエスケープ処理をして文字列として扱えるか検証してみましたが上手く行きません。
|
202
|
+
|
203
|
+
何故かわからないです。ご教示頂ければ幸いです。
|
204
|
+
|
205
|
+
```java
|
206
|
+
|
207
|
+
static BufferedReader _br = new BufferedReader(new InputStreamReader(System.in));
|
208
|
+
|
209
|
+
// final String SQL = "Select EMPID, ENAME, JOB from EMPLOYEES ";
|
210
|
+
|
211
|
+
public static void main(String[] args) {
|
212
|
+
|
213
|
+
System.out.println("検索したい文字をを入力して下さい。");
|
214
|
+
|
215
|
+
System.out.println("入力する内容は正しいSQL文、かつ、選択したテーブルに則した内容でなければならない。");
|
216
|
+
|
217
|
+
System.out.println("Select EMPID, ENAME, JOB from EMPLOYEES where JOB like");
|
218
|
+
|
219
|
+
System.out.println("");
|
220
|
+
|
221
|
+
DDD d = new DDD();
|
222
|
+
|
223
|
+
boolean flg = true;
|
224
|
+
|
225
|
+
String word = null;
|
226
|
+
|
227
|
+
while (flg) {
|
228
|
+
|
229
|
+
try {
|
230
|
+
|
231
|
+
// input where clause
|
232
|
+
|
233
|
+
word = _br.readLine();
|
234
|
+
|
235
|
+
d.getName(word);
|
236
|
+
|
237
|
+
flg = false;
|
238
|
+
|
239
|
+
} catch (IOException e) {
|
240
|
+
|
241
|
+
System.out.println("入力された値は不正です。");
|
242
|
+
|
243
|
+
continue;
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
private void getName(String word){
|
252
|
+
|
253
|
+
Connection con = null;
|
254
|
+
|
255
|
+
String sql = null;
|
256
|
+
|
257
|
+
PreparedStatement ps = null;
|
258
|
+
|
259
|
+
ResultSet rs = null;
|
260
|
+
|
261
|
+
String hostname = "*****";
|
262
|
+
|
263
|
+
String username = "****";
|
264
|
+
|
265
|
+
String pw = "******";
|
266
|
+
|
267
|
+
String sid = "XE";
|
268
|
+
|
269
|
+
String url = "jdbc:oracle:thin:@";
|
270
|
+
|
271
|
+
try{
|
272
|
+
|
273
|
+
Class.forName("oracle.jdbc.driver.OracleDriver");
|
274
|
+
|
275
|
+
con = DriverManager.getConnection(url + hostname + ":1521:" + sid, username, pw);
|
276
|
+
|
277
|
+
sql = "Select EMPID, ENAME, JOB from EMPLOYEES where JOB like?";
|
278
|
+
|
279
|
+
ps = con.prepareStatement(sql);
|
280
|
+
|
281
|
+
ps.setString(1, "%" +this.escape(word) + "%");
|
282
|
+
|
283
|
+
rs = ps.executeQuery();
|
284
|
+
|
285
|
+
while (rs.next()) {
|
286
|
+
|
287
|
+
// System.out.println(rs.getInt("EMPID")+rs.getString("ENAME")+rs.getString("JOB"));
|
288
|
+
|
289
|
+
System.out.println("EMPID " + rs.getInt("EMPID") + " ENAME " + rs.getString("ENAME") + " JOB "
|
290
|
+
|
291
|
+
+ rs.getString("JOB"));
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
rs.close();
|
302
|
+
|
303
|
+
ps.close();
|
304
|
+
|
305
|
+
} catch (ClassNotFoundException e) {
|
306
|
+
|
307
|
+
System.out.println("There is no JDBC driver");
|
308
|
+
|
309
|
+
} catch (SQLException e) {
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
e.printStackTrace();
|
314
|
+
|
315
|
+
}finally{
|
316
|
+
|
317
|
+
if(con != null){
|
318
|
+
|
319
|
+
try {
|
320
|
+
|
321
|
+
con.close();
|
322
|
+
|
323
|
+
} catch (SQLException e) {
|
324
|
+
|
325
|
+
System.out.println( e.getErrorCode() );
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
public String escape(String before){
|
340
|
+
|
341
|
+
StringBuilder after = new StringBuilder();
|
342
|
+
|
343
|
+
String esSymbol = "\";
|
344
|
+
|
345
|
+
char es1 = '_';
|
346
|
+
|
347
|
+
char es2 = '%';
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
for (int i = 0; i < before.length(); i++) {
|
352
|
+
|
353
|
+
if (before.charAt(i) == es1 || before.charAt(i) == es2) {
|
354
|
+
|
355
|
+
after.append(esSymbol);
|
356
|
+
|
357
|
+
after.append(String.valueOf(before.charAt(i)));
|
358
|
+
|
359
|
+
continue;
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
after.append(String.valueOf(before.charAt(i)));
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
return after.toString();
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
```
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
###期待結果:
|
380
|
+
|
381
|
+
入力した値が正しいかわからないのですが、
|
382
|
+
|
383
|
+
以下の値を入力して検索をかけて
|
384
|
+
|
385
|
+
%事務\_チームA%
|
386
|
+
|
387
|
+
![エスケープ処理](bfe03d4dc8a3f7327c9b359e366a151f.gif)
|