Java equivalent for PHP's mysql_real_escape_string()

后端 未结 7 671
温柔的废话
温柔的废话 2021-01-11 10:17

Is there a Java equivalent to PHP\'s mysql_real_escape_string() ?

This is to escape SQL injection attempts before passing them to Statement.execute().

I know

7条回答
  •  难免孤独
    2021-01-11 10:38

    I would not trust anything else than PreparedStatement to ensure security. But if you need to have a similar workflow when building queries you may use the code below. It uses a PreparedStatement underneath, works like a StringBuilder, adds escape functions and tracks the parameter indexes for you. It can be used like this:

    SQLBuilder sqlBuilder = new SQLBuilder("update ").append(dbName).append(".COFFEES ");
    sqlBuilder.append("set SALES = ").escapeString(sales);
    sqlBuilder.append(", TOTAL = ").escapeInt(total);
    sqlBuilder.append("where COF_NAME = ").escapeString(coffeeName);
    sqlBuilder.prepareStatement(connection).executeUpdate();
    

    Here's the code:

    class SQLBuilder implements Appendable {
        private StringBuilder sqlBuilder;
        private List values = new ArrayList<>();
    
        public SQLBuilder() {
            sqlBuilder = new StringBuilder();
        }
    
        public SQLBuilder(String str)
        {
            sqlBuilder = new StringBuilder(str);
        }
    
        @Override
        public SQLBuilder append(CharSequence csq)
        {
            sqlBuilder.append(csq);
            return this;
        }
    
        @Override
        public SQLBuilder append(CharSequence csq, int start, int end)
        {
            sqlBuilder.append(csq, start, end);
            return this;
        }
    
        @Override
        public SQLBuilder append(char c)
        {
            sqlBuilder.append(c);
            return this;
        }
    
        // you can add other supported parameter types here...
        public SQLBuilder escapeString(String x)
        {
            protect(x);
            return this;
        }
    
        public SQLBuilder escapeInt(int x)
        {
            protect(x);
            return this;
        }
    
        private void escape(Object o)
        {
            sqlBuilder.append('?');
            values.add(o);
        }
    
        public PreparedStatement prepareStatement(Connection connection)
            throws SQLException
        {
            PreparedStatement preparedStatement =
                connection.prepareStatement(sqlBuilder.toString());
            for (int i = 0; i < values.size(); i++)
            {
                Object value = values.get(i);
                // you can add other supported parameter types here...
                if (value instanceof String)
                    preparedStatement.setString(i + 1, (String) value);
                else if (value instanceof Integer)
                    preparedStatement.setInt(i + 1, (Integer) value);
            }
            return preparedStatement;
        }
    
        @Override
        public String toString()
        {
            return "SQLBuilder: " + sqlBuilder.toString();
        }
    }
    
        

    提交回复
    热议问题