I have query similar to this one:
"SELECT COUNT(*) FROM EMPLOYEE WHERE ADDRESS IN (" + listOfStringsSeparatedByComma + ")"
and the tools complain that there is potential for SQL injection.
In order to solve this, I saw that I can use this in Spring JDBC:
SqlParameterSource namedParameters = new MapSqlParameterSource()
.addValue("addresses", Arrays.asList("Canada", "USA", "Finland"));
return namedParameterJdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE WHERE ADDRESS IN (:addresses)",
namedParameters, Integer.class);
and it seems to work, since the output I get when I run this is:
Executing prepared SQL statement [SELECT COUNT(*) FROM EMPLOYEE WHERE ADDRESS IN (?, ?, ?)]
Setting SQL statement parameter value: column index 1, parameter value [Canada], value class [java.lang.String], SQL type unknown
Setting SQL statement parameter value: column index 2, parameter value [USA], value class [java.lang.String], SQL type unknown
Setting SQL statement parameter value: column index 3, parameter value [Finland], value class [java.lang.String], SQL type unknown
Now, my question is - can I somehow disable generation of prepared statement (or I guess it has to be prepared statement - but then to disable caching of it), since if number of parameters in namedParameters
varies a lot, I will have a ton of prepared statements and then I will have a complaint from MySQL about too many prepared statements in cache.
Basically I want to have benefit of not handling possible SQL injection, and at a same time not having MySQL complaining about too many prepared statements generated.