Here you can find the source of wildcardUserToSql(String exp)
Parameter | Description |
---|---|
exp | the SQL LIKE parameter |
public static String wildcardUserToSql(String exp)
//package com.java2s; /*//from ww w . ja v a 2s. c o m * Copyright (C) 2002-2017 FlyMine * * This code may be freely distributed and modified under the * terms of the GNU Lesser General Public Licence. This should * be distributed with the code. See the LICENSE file for more * information or http://www.gnu.org/copyleft/lesser.html. * */ public class Main { /** * Turn a user supplied wildcard expression with * into an SQL LIKE/NOT LIKE * expression with %'s and other special characters. Please note that constraint * value is saved in created java object (constraint) in form with '%' and in * this form is saved in xml as well. * * @param exp the SQL LIKE parameter * @return the equivalent wildcard expression */ public static String wildcardUserToSql(String exp) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < exp.length(); i++) { String substring = exp.substring(i); if (substring.startsWith("*")) { sb.append("%"); } else if (substring.startsWith("?")) { sb.append("_"); } else if (substring.startsWith("\\*")) { sb.append("*"); i++; } else if (substring.startsWith("\\?")) { sb.append("?"); i++; } else if (substring.startsWith("%")) { sb.append("\\%"); } else if (substring.startsWith("_")) { sb.append("\\_"); } else if (substring.startsWith("\\")) { sb.append("\\\\"); } else { sb.append(substring.charAt(0)); } } return sb.toString(); } }