How to avoid “Security - A prepared statement is generated from a nonconstant String” FindBugs Warning

前端 未结 7 3257
灰色年华
灰色年华 2021-02-19 21:39

I am working on a project that has a piece of code like the one below:

String sql = \"SELECT MAX(\" + columnName + \") FROM \" + tableName;                
Prepa         


        
7条回答
  •  星月不相逢
    2021-02-19 22:00

    private static final String SQL = "SELECT MAX(?) FROM ?";
    PreparedStatement ps = connection.prepareStatement(sql);
    ps.preparedStatement.setInt(1,columnName);
    ps.preparedStatement.setString(2,tableName);
    

    if you are using prepared statement, then in parameter should be a final string and parameters should be added later using setInt, setString methods.

    this will resolve the findbug warning.

提交回复
热议问题