2017-08-10 55 views
1

我有两个值picIdtubId我想更新表A.但首先,我必须检查它们是否是空的,即""。我使用两次更新语句。我想知道是否只能使用update语句来完成此任务。Oracle11g sql可以更新语句判断一些列是“”

String picId = ""; 
String tubId = "1"; 

if(!"".equals(picId)) { 
    String update = "update A set columnName=someValue where id=" + picId; 
    //this method for update 
    this.executeUpdate(update, new Object[]{}); 
} 
if(!"".equals(tubId)) { 
    String update = "update A set columnName=someValue where id=" + tubId; 
    this.executeUpdate(update, new Object[]{}); 
} 

非常感谢!

+0

此代码易受SQL注入攻击。 [使用PreparedStatements代替](https://stackoverflow.com/questions/1812891/java-escape-string-to-prevent-sql-injection)。 – Michael

+0

谢谢all.WIth你的帮助我已经知道如何处理它: –

回答

0

尊敬的琼恩·雪诺尝试使用in这样的:

String update = "update A set columnName=someValue where id in (" + tubId +","+picId+")"; 
0

在这里,你可以使用另一个变量,它给的条件基础。

String picId=""; 
String tubId="1"; 
String sample=""; 
if(!(picID.equals(""))) 
{ 
sample=picId; 
} 
else if(!(tubId.equals(""))) 
{ 
sample=tubId; 
} 
String update = "update A set columnName=someValue where id=" + sample; 

     this.executeUpdate(update, new Object[]{}); 
+0

但如果他们都不是“”或他们都是“”? –

0

我希望你知道在SQL查询中使用串联是SQL注入的方式。

但是关于你的问题,你可以在WHERE子句中使用IN条件。只准备数据:

List<String> listId = new ArrayList<>(); 
if (!"".equals(picId)) { 
    listId.add(picId); 
} 
if(!"".equals(tubId)) { 
    listId.add(tubId); 
} 
String ids = listId.stream().map(_s -> String.format("'%s'", _s)).collect(Collectors.joining(",", "(", ")")); 
String update = "update A set columnName=someValue where id IN " + ids; 
this.executeUpdate(update, new Object[]{}); 
+0

为了避免SQL注入,我改进了这个语句。并且在您的帮助下,我知道如何在SQL中使用IN关键字 –

+0

我很高兴,它很有用。我会使用准备好的语句来执行查询。没有人知道SQL注入是如何执行的。 – Syroezhkin