zoukankan      html  css  js  c++  java
  • util之sql拼接工具

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * @Author crd
     * @Date 2020-11-4 下午5:52:35
     * @Version 1.0 业务说明:主要用于oracle拼接sql,handleNull方法的参数说明,第二个布尔值是指是否需要添加逗号
     * 
     */
    public  class SqlAddUtil {
        public static String handleNull(Date d, Boolean isNeedComma) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String s = null;
            if (d != null) {
                if (isNeedComma) {
                    s = "to_date('" + sdf.format(d) + "', 'YYYYMMdd')" + ",";
                } else {
                    s = "to_date('" + sdf.format(d) + "', 'YYYYMMdd')";
                }
            } else {
                if (isNeedComma) {
                    s = null + ",";
                } else {
                    s = null;
                }
            }
            return s;
        }
    
        public static String handleNull(String str, Boolean isNeedComma) {
            String s = null;
            if (str != null) {
                if (isNeedComma) {
                    s = "'" + str + "'" + ",";
                } else {
                    s = "'" + str + "'";
                }
            } else {
                if (isNeedComma) {
                    s = null + ",";
                } else {
                    s = null;
                }
            }
    
            return s;
        }
    
        public static String handleNull(Double d, Boolean isNeedComma) {
            String s = "";
            if (d != null) {
    
                if (isNeedComma) {
                    s = d + ",";
                } else {
                    s = d + "";
                }
    
            } else {
                if (isNeedComma) {
                    s = null + ",";
                } else {
                    s = null;
                }
            }
            return s;
        }
    
        public static String handleNull(Float f, Boolean isNeedComma) {
            String s = "";
            if (f != null) {
                if (isNeedComma) {
                    s = f + ",";
                } else {
                    s = f + "";
                }
            } else {
                if (isNeedComma) {
                    s = null + ",";
                } else {
                    s = null;
                }
            }
            return s;
        }
    
        public static String handleNull(Long l, Boolean isNeedComma) {
            String s = "";
            if (l != null) {
    
                if (isNeedComma) {
                    s = l + ",";
                } else {
                    s = l + "";
                }
            } else {
                if (isNeedComma) {
                    s = null + ",";
                } else {
                    s = null;
                }
            }
            return s;
        }
    
        public static String handleNull(Integer i, Boolean isNeedComma) {
            String s = "";
            if (i != null) {
    
                if (isNeedComma) {
                    s = i + ",";
                } else {
                    s = i + "";
                }
            } else {
                if (isNeedComma) {
                    s = null + ",";
                } else {
                    s = null;
                }
            }
            return s;
        }
    
    }

    功能:主要用于oracle拼接sql,handleNull方法的参数说明,第二个布尔值是指是否需要添加逗号

    方法

    使用列子:

            String sql = "INSERT INTO W_FHYCGZ(MY_ID_,WDID,RQ,XQ,SJWDZG,SJWDZD,TQ,FHID,ZDFHZG,ZDFHZD,TDFHZG,TDFHZD,QWGDL,ZBGDL,ZDEMSGDFH,TDXSDDL,ZDTDXSDDL,ZDFDL,ZXFDL,ZDGFDL,GF14SDL,BZ,ZT,BYZD,BYZDDATE) VALUES " 
                            + "("   + handleNull(e.getMyId(),true) 
                                    + handleNull(e.getWdid(),true) 
                                    + handleNull(e.getRq(),true)  
                                    + handleNull(e.getXq(),true)  
                                    + handleNull(e.getSjwdzg(),true)  
                                    + handleNull(e.getSjwdzd(),true) 
                                    + handleNull(e.getTq(),true)  
                                    + handleNull(e.getFhid(),true)  
                                    + handleNull(e.getZdfhzg(),true)  
                                    + handleNull(e.getZdfhzd(),true)
                                    + handleNull(e.getTdfhzg(),true) 
                                    + handleNull(e.getTdfhzd(),true)
                                    + handleNull(e.getQwgdl(),true) 
                                    + handleNull(e.getZbgdl(),true)  
                                    + handleNull(e.getZdemsgdfh(),true)  
                                    + handleNull(e.getTdxsddl(),true)  
                                    + handleNull(e.getZdtdxsddl(),true)  
                                    + handleNull(e.getZdfdl(),true)  
                                    + handleNull(e.getZxfdl(),true)  
                                    + handleNull(e.getZdgfdl(),true)  
                                    + handleNull(e.getGf14sdl(),true)  
                                    + handleNull(e.getBz(),true)  
                                    + handleNull(e.getZt(),true)  
                                    + handleNull(e.getByzd(),true) 
                                    + handleNull(e.getByzddate(),false) 
                                    + ")";
                            System.out.println("==================================================="+sql);
                            jdbcTemplate.execute(sql);
  • 相关阅读:
    【你的职业规划】web前端的职业发展方向及学习攻略【转载】
    开发人员如何高效编程【转载】
    微信小程序开发——以简单易懂的浏览器页面栈理解小程序的页面路由
    微信小程序开发——超链接或按钮点击跳转到其他页面失效
    小程序开发常见异常收集整理
    微信小程序开发——开发者工具无法输入中文的处理
    微信小程序开发——前端如何区分小程序运行环境
    微信小程序开发——setData的使用技巧
    微信小程序开发——打开另一个小程序
    微信小程序开发——全局配置详细介绍
  • 原文地址:https://www.cnblogs.com/rdchen/p/13927188.html
Copyright © 2011-2022 走看看