Java - escape string to prevent SQL injection

前端 未结 12 2436
庸人自扰
庸人自扰 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 02:25

    From:[Source]

    public String MysqlRealScapeString(String str){
      String data = null;
      if (str != null && str.length() > 0) {
        str = str.replace("\\", "\\\\");
        str = str.replace("'", "\\'");
        str = str.replace("\0", "\\0");
        str = str.replace("\n", "\\n");
        str = str.replace("\r", "\\r");
        str = str.replace("\"", "\\\"");
        str = str.replace("\\x1a", "\\Z");
        data = str;
      }
    return data;
    

    }

提交回复
热议问题