Java tutorial
//package com.java2s; /* * Copyright 2011 The Kuali Foundation. * * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php * * 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. */ import java.util.Collection; public class Main { /** * * build a string of placeholders for a parameterized java.sql IN clause * @param parameterCount the number of parameters in the IN clause * @return the String (?,?,?) with the correct nubmer of parameters */ protected static String inString(Integer parameterCount) { // there should be at least one parameter in the IN string // but allow people to screw up and have an IN condition which is never satisfied if (parameterCount == 0) { return new String("('')"); } StringBuilder sb = new StringBuilder(20); sb = sb.append("(?"); for (int i = 1; i < parameterCount; i++) { sb.append(",?"); } sb.append(")"); return sb.toString(); } /** * * build a SQL IN clause from the array of parameters passed in * @param inListValues: components of the IN list * @return an empty string if the IN list will be empty */ protected static String inString(Collection<String> inListValues) { if (inListValues.isEmpty()) { return ""; } // the delimiter for strings in the DB is assumed to be a single quote. // this is the ANSI-92 standard. // if the ArrayList input is empty, IN ('') is returned. StringBuilder inBuilder = new StringBuilder(150); inBuilder.append("('"); boolean isFirst = true; for (String val : inListValues) { if (isFirst) { isFirst = false; } else { inBuilder.append("','"); } inBuilder.append(val); } inBuilder.append("')"); return inBuilder.toString(); } }