jsp自动拼接SQL语句实例

发布时间:2020-01-03编辑:脚本学堂
本文介绍了jsp编程中,自动拼接sql语句的例子,有需要的朋友参考下。

例子,自动拼接sql语句的代码。
 

复制代码 代码示例:
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 
/**
 * @author QuarterLifeForJava
 */ 
public class Test{ 
 
    public static void main(String[] args) { 
        System.out.println(testPrintSQL("H002",12,1009)); 
        String sql = "SELECT rat.id,rat.sName,rat.dApplyDate,rat.tiRestType,rat.dStartDate,rat.dEndDate,rat.fInterval "+ 
                 "FROM restapplytbl rat "+ 
                 "WHERE rat.id IN"+ 
                 "("+ 
                 "  SELECT rt.iRestApplyId "+ 
                 "  FROM restprocesstbl rt "+ 
                 "  WHERE rt.id IN"+ 
                 "  ("+ 
                 "      SELECT pmt.smissionid "+ 
                 "      FROM pendingmissiontbl pmt "+ 
                 "      WHERE pmt.imissionkindid=?"+ 
                 "      AND pmt.imissionkinddetailsid=?"+ 
                 "      AND pmt.smissionexecutor=?"+ 
                 "  )"+ 
                 ")"; 
        String worknum = "H002"; 
        int missionKindId = 12; 
        int missionKindDetailsId = 1009; 
        //顺序要对好 
        Object obj[] = new Object[]{missionKindId,missionKindDetailsId,worknum};  
        System.out.println(createSql(sql,obj)); 
    } 
 
    //简单自动拼接SQL语句 
    public static String createSql(String sql, Object pargams[]) { 
        for (int i = 0; i < pargams.length; i++) { 
            Pattern p = Pattern.compile("?"); 
            Matcher m = p.matcher(sql); 
            if (pargams[i] instanceof Integer) { 
                sql = m.replaceFirst(String.valueOf((int) pargams[i])); 
            } else if (pargams[i] instanceof Long) { 
                sql = m.replaceFirst(String.valueOf((Long) pargams[i])); 
            } else if (pargams[i] instanceof Float) { 
                sql = m.replaceFirst(String.valueOf((Float) pargams[i])); 
            } else if (pargams[i] instanceof Double) { 
                sql = m.replaceFirst(String.valueOf((Double) pargams[i])); 
            } else { 
                sql = m.replaceFirst("'" + (String) pargams[i] + "'"); 
            } 
        } 
        return sql; 
    } 
     
    //简单示例 
    public static String testPrintSQL(String worknum, int missionKindId,int missionKindDetailsId){ 
        String sql = "SELECT rat.id,rat.sName,rat.dApplyDate,rat.tiRestType,rat.dStartDate,rat.dEndDate,rat.fInterval "+ 
                     "FROM restapplytbl rat "+ 
                     "WHERE rat.id IN"+ 
                     "("+ 
                     "  SELECT rt.iRestApplyId "+ 
                     "  FROM restprocesstbl rt "+ 
                     "  WHERE rt.id IN"+ 
                     "  ("+ 
                     "      SELECT pmt.smissionid "+ 
                     "      FROM pendingmissiontbl pmt "+ 
                     "      WHERE pmt.imissionkindid="+missionKindId+ 
                     "      AND pmt.imissionkinddetailsid="+missionKindDetailsId+ 
                     "      AND pmt.smissionexecutor="+"'"+worknum+"'"+ 
                     "  )"+ 
                     ")"; 
        return sql; 
    }