Java IntelliJ IDEA解决方案检查报告

Java IntelliJ IDEA解决方案检查报告,java,intellij-idea,inspect,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("

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


如何解决它?

检查提示您最好使用准备好的语句。因此,您不需要像
updateuserssetname=“test”WHERE id='1'
这样的查询,而需要像
updateuserssetname=?其中id=?
,并在执行之前传递所需的参数

因此,在你的情况下:

PreparedStatement pst=con.PreparedStatement(sql);
//如果需要,请调用pst.setInt()、pst.setString()等
pst.executeUpdate()

请参阅本教程中的更多详细信息和示例: