Java String Quote quoteSQL(String string)

Here you can find the source of quoteSQL(String string)

Description

Puts the given String into quotation marks, original quotation marks are escaped

License

Open Source License

Parameter

Parameter Description
string the original String

Return

the new quoted String

Declaration

public static String quoteSQL(String string) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006-2013, Cloudsmith Inc.
 * The code, documentation and other materials contained herein have been
 * licensed under the Eclipse Public License - v 1.0 by the copyright holder
 * listed above, as the Initial Contributor under such license. The text or
 * such license is available at www.eclipse.org.
 ******************************************************************************/

public class Main {
    /**//from w  w  w.j a v a2 s . com
     * Puts the given String into quotation marks, original quotation marks are
     * escaped
     * 
     * @param string
     *            the original String
     * @return the new quoted String
     */
    public static String quoteSQL(String string) {
        if (string == null)
            return "NULL"; //$NON-NLS-1$

        return "'" + escapeSQL(string, false) + "'"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Puts the given String into quotation marks, original quotation marks are
     * escaped, according to the second parameter backslashes can be escaped too
     * 
     * @param string
     *            the original String
     * @param escapeBackslashes
     *            true = duplicate backslashes
     * @return the new quoted String
     */
    public static String quoteSQL(String string, boolean escapeBackslashes) {
        if (string == null)
            return "NULL"; //$NON-NLS-1$

        return "'" + escapeSQL(string, escapeBackslashes) + "'"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Duplicates quotation marks
     * 
     * @param string
     *            the original String to be escaped
     * @return the new String
     */
    public static String escapeSQL(String string) {
        return escapeSQL(string, false);
    }

    /**
     * Duplicates quotation marks and backslashes (according to the second
     * parameter)
     * 
     * @param string
     *            the original String to be escaped
     * @param escapeBackslashes
     *            true = duplicate backslashes
     * @return the new String
     */
    public static String escapeSQL(String string, boolean escapeBackslashes) {
        if (string == null)
            return null;

        if (escapeBackslashes) {
            return string.replaceAll("'", "''").replaceAll("\\\\", "\\\\\\\\"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        }

        return string.replaceAll("'", "''"); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

Related

  1. quotes(String str)
  2. quotesEscape(String text)
  3. QuoteSingle(String S)
  4. quoteSpecial(String orig)
  5. quoteSpecialCharacters(final String oldString)
  6. quoteSqlIdentifier(String identifier)
  7. quoteSqlIdentifier(String name)
  8. quoteSQLString(String s)
  9. quoteStr(Object o)