IntelliJ IDEA解决检查报告

时间:2016-01-20 04:26:28

标签: java intellij-idea inspect

IntelliJ IDEA 15.0.2

这是我的代码段:

    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语句是安全漏洞的常见来源

我该如何解决?

1 个答案:

答案 0 :(得分:1)

检查暗示您最好使用准备好的声明。因此,与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