Eliminating the problem
Posted Jun 1, 2006 14:50 UTC (Thu) by
mrshiny (subscriber, #4266)
In reply to:
Eliminating the problem by iabervon
Parent article:
SQL injection vulnerabilities in PostgreSQL
I'd still feel better using a prepared statement, even if there are optional clauses in the statement. In such cases I normally do something like this:
List args = new ArrayList();
String sql = "select * from daily_revenue where transaction type = ? ";
args.add(transType);
if (sinceLastLogin) {
sql += " and trans_date >= ? "; // for lots of optional clauses, use StringBuffer
args.add(lastLoginDate);
}
Then later on you just bind all the variables in the order they appeared in the list. Also you can use a var-args function (in Java 1.5 and other languages that support var-args) to automate things like date converstion; if the type of one of the objects in the args list is Date or Calendar or some other non-SQL-friendly type, you can convert it automatically.
(
Log in to post comments)