2016-01-20 79 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