Here you can find the source of quoteStringSQL(String s)
Parameter | Description |
---|---|
s | the text to convert. |
public static String quoteStringSQL(String s)
//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(); } }