Here you can find the source of quoteSQL(String string)
Parameter | Description |
---|---|
string | the original String |
public static String quoteSQL(String string)
//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$ } }