Java - escape string to prevent SQL injection

前端 未结 12 2407
庸人自扰
庸人自扰 2020-11-22 01:58

I\'m trying to put some anti sql injection in place in java and am finding it very difficult to work with the the \"replaceAll\" string function. Ultimately I need a functio

12条回答
  •  不要未来只要你来
    2020-11-22 02:23

    Prepared Statements are the best solution, but if you really need to do it manually you could also use the StringEscapeUtils class from the Apache Commons-Lang library. It has an escapeSql(String) method, which you can use:

    import org.apache.commons.lang.StringEscapeUtils; … String escapedSQL = StringEscapeUtils.escapeSql(unescapedSQL);

提交回复
热议问题