正在从 commons.lang 迁移 StringEscapeUtils.escapeSql
Migrating StringEscapeUtils.escapeSql from commons.lang
我已经开始将 commons.lang 2 迁移到 commons.lang3。
根据
https://commons.apache.org/proper/commons-lang/article3_0.html
StringEscapeUtils.escapeSql
This was a misleading method, only handling the simplest of possible SQL cases. >As SQL is not Lang's focus, it didn't make sense to maintain this method.
明白了,但是推荐用什么代替呢?
澄清
您能否推荐执行类似于 StringEscapeUtils.escapeSql 的简单 escapeSql 的第三方?
At present, this method only turns single-quotes into doubled single-quotes ("McHale's Navy" => "McHale''s Navy").
这是方法代码:
/**
675 * <p>Escapes the characters in a <code>String</code> to be suitable to pass to
676 * an SQL query.</p>
677 *
678 * <p>For example,
679 * <pre>statement.executeQuery("SELECT * FROM MOVIES WHERE TITLE='" +
680 * StringEscapeUtils.escapeSql("McHale's Navy") +
681 * "'");</pre>
682 * </p>
683 *
684 * <p>At present, this method only turns single-quotes into doubled single-quotes
685 * (<code>"McHale's Navy"</code> => <code>"McHale''s Navy"</code>). It does not
686 * handle the cases of percent (%) or underscore (_) for use in LIKE clauses.</p>
687 *
688 * see http://www.jguru.com/faq/view.jsp?EID=8881
689 * @param str the string to escape, may be null
690 * @return a new String, escaped for SQL, <code>null</code> if null string input
691 */
692 public static String escapeSql(String str) {
693 if (str == null) {
694 return null;
695 }
696 return StringUtils.replace(str, "'", "''");
697 }
所以您可以通过简单调用 String#replace
.
轻松替换该方法
但是,删除该方法是有原因的。它真的是半生不熟,我想不出你想要使用它的充分理由。例如,对于 运行 JDBC 查询,您可以而且应该使用绑定变量,而不是尝试插入和转义字符串文字。
如果您使用的是 JDBC 连接,请准备一个包含如下参数的语句:
con.prepareStatement("INSERT INTO table1 VALUES (?,?)");
pstmt.setInt(1, 200);
pstmt.setString(2, "Julie");
pstmt.executeUpdate();
您无需转义使用预准备语句中的函数插入的任何元素。那些是自动转义的。
这已在之前回答过:
Java - escape string to prevent SQL injection
OWASP 有一个API,叫做ESAPI,提供了其中的一些功能,你可以看看。
我已经开始将 commons.lang 2 迁移到 commons.lang3。
根据 https://commons.apache.org/proper/commons-lang/article3_0.html
StringEscapeUtils.escapeSql
This was a misleading method, only handling the simplest of possible SQL cases. >As SQL is not Lang's focus, it didn't make sense to maintain this method.
明白了,但是推荐用什么代替呢?
澄清
您能否推荐执行类似于 StringEscapeUtils.escapeSql 的简单 escapeSql 的第三方?
At present, this method only turns single-quotes into doubled single-quotes ("McHale's Navy" => "McHale''s Navy").
这是方法代码:
/**
675 * <p>Escapes the characters in a <code>String</code> to be suitable to pass to
676 * an SQL query.</p>
677 *
678 * <p>For example,
679 * <pre>statement.executeQuery("SELECT * FROM MOVIES WHERE TITLE='" +
680 * StringEscapeUtils.escapeSql("McHale's Navy") +
681 * "'");</pre>
682 * </p>
683 *
684 * <p>At present, this method only turns single-quotes into doubled single-quotes
685 * (<code>"McHale's Navy"</code> => <code>"McHale''s Navy"</code>). It does not
686 * handle the cases of percent (%) or underscore (_) for use in LIKE clauses.</p>
687 *
688 * see http://www.jguru.com/faq/view.jsp?EID=8881
689 * @param str the string to escape, may be null
690 * @return a new String, escaped for SQL, <code>null</code> if null string input
691 */
692 public static String escapeSql(String str) {
693 if (str == null) {
694 return null;
695 }
696 return StringUtils.replace(str, "'", "''");
697 }
所以您可以通过简单调用 String#replace
.
但是,删除该方法是有原因的。它真的是半生不熟,我想不出你想要使用它的充分理由。例如,对于 运行 JDBC 查询,您可以而且应该使用绑定变量,而不是尝试插入和转义字符串文字。
如果您使用的是 JDBC 连接,请准备一个包含如下参数的语句:
con.prepareStatement("INSERT INTO table1 VALUES (?,?)");
pstmt.setInt(1, 200);
pstmt.setString(2, "Julie");
pstmt.executeUpdate();
您无需转义使用预准备语句中的函数插入的任何元素。那些是自动转义的。
这已在之前回答过: Java - escape string to prevent SQL injection
OWASP 有一个API,叫做ESAPI,提供了其中的一些功能,你可以看看。