2016-01-20 77 views
1

的IntelliJ IDEA 15.0.2IntelliJ IDEA的解决检查报告

这里是我的代码片段:

public int update(final String sql){ 
    int res = -1; 
    try (Connection con = this.connect(); 
     Statement pst = con.createStatement()){ 
     res=pst.executeUpdate(sql);//**if write pst.executeUpdate("") than no inspect message** 
    } catch (final SQLException e) { 
     this.logger.log(Level.SEVERE,e.getMessage()); 
    } 
    return res; 
} 

下面是检查邮件:

电话“的Statement.executeUpdate() '与非恒定参数(短消息)

更多

将调用java.sql.Statement.executeUpdate()或其任何采用动态构造字符串的变体报告为要执行的查询。构造的SQL语句是安全漏洞的常见来源

如何解决它?

回答

0

检查提示您最好使用准备好的语句。因此,而不是像UPDATE users SET name = "test" WHERE id ='1'这样的查询,您将最终与像UPDATE users SET name = ? WHERE id = ?查询,并在执行它之前传递所需的参数。

所以你的情况:

PreparedStatement pst = con.prepareStatement(sql); // call pst.setInt(), pst.setString() etc. if needed pst.executeUpdate();

请参见本教程更多细节和例子:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html