Java String Quote quoteStringSQL(String s)

Here you can find the source of quoteStringSQL(String s)

Description

Convert a string to a SQL literal.

License

Apache License

Parameter

Parameter Description
s the text to convert.

Return

the SQL literal

Declaration

public static String quoteStringSQL(String s) 

Method Source Code

//package com.java2s;
/*//  ww  w. j av  a2s. com
 * Copyright 2014-2016 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the ?License??);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an ?AS IS?? BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Convert a string to a SQL literal. Null is converted to NULL. The text is
     * enclosed in single quotes. If there are any special characters, the
     * method STRINGDECODE is used.
     *
     * @param s the text to convert.
     * @return the SQL literal
     */
    public static String quoteStringSQL(String s) {
        if (s == null) {
            return "NULL";
        }
        int length = s.length();
        StringBuilder buff = new StringBuilder(length + 2);
        buff.append('\'');
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c == '\'') {
                buff.append(c);
            } /*else if (c < ' ' || c > 127) {
              // need to start from the beginning because maybe there was a \
              // that was not quoted
              return "STRINGDECODE(" + quoteStringSQL(javaEncode(s)) + ")";
              }*/
            buff.append(c);
        }
        buff.append('\'');
        return buff.toString();
    }
}

Related

  1. quoteString(String strVal)
  2. quoteString(String t)
  3. quoteString(String unquoted)
  4. quoteString(String value, boolean force)
  5. quoteStringLiteral(String string)
  6. quoteStringSQL(String s)
  7. quoteStringValue(String value)
  8. quoteText(String text)
  9. quoteText(String textToQuote, boolean sbAppend)