• 动态SQL拼接,select,insert的写法


     1 import java.util.Iterator;
     2 import java.util.Map;
     3 import java.util.Set;
     4 
     5 public class SQLUtil {
     6     public static String sqlSelect = "";
     7 
     8     public static String selectQuerySQL(Map<String, Object> map, String tableName) {
     9         if (map.isEmpty() || map.size() == 0) {
    10             sqlSelect = "select*from" + tableName;
    11         } else {
    12             sqlSelect = "select*from" + tableName + "where";
    13             Set set = map.keySet();
    14             Iterator iterator = set.iterator();
    15             int index = 0;
    16             while (iterator.hasNext()) {
    17                 String key = (String) iterator.next();
    18                 Object value = map.get(key);
    19                 if (value instanceof String) {
    20                     value = "'" + value + "'";
    21                 }
    22                 index++;
    23                 if ((index == 1 && map.size() == 1) || map.size() == index) {
    24                     sqlSelect += key + "=" + value + " ";
    25                 } else {
    26                     sqlSelect += key + "=" + value + " " + "and" + " ";
    27                 }
    28             }
    29         }
    30         System.out.println("待执行的select SQL:"+sqlSelect);
    31         return sqlSelect;
    32     }
    33     public static String insertSQL(Map<String,Object>map){
    34         Iterator<String> iterator = map.keySet().iterator();
    35         StringBuilder sqlTable=new StringBuilder("insert into");
    36         StringBuilder sqlKey=new StringBuilder("(");
    37         StringBuilder sqlVaule= new StringBuilder("value(");
    38         while (iterator.hasNext()){
    39             String key=iterator.next();
    40             if(map.get(key).toString()==null||map.get(key).toString().length()==0){
    41                 iterator.remove();
    42             }else {
    43                 if (key.equals("tableName")){
    44                     sqlTable.append(map.get(key));
    45                 }else {
    46                     sqlKey.append(key+",");
    47                     sqlVaule.append(map.get(key)+",");
    48                 }
    49             }
    50         }
    51         sqlTable.append(sqlKey.toString().substring(0,sqlKey.toString().lastIndexOf(","))+")").append(sqlVaule.toString().substring(0,sqlVaule.lastIndexOf(","))+")");
    52         String sql=sqlTable.toString();
    53         System.out.println("待执行的insert SQL:"+sql);
    54         return sql;
    55     }
    56     /*此为以上方法的用法,可以忽略*/
    57     /*public static void main(String[] args) {
    58         //调用select的方法
    59         Map<String,Object> map=new HashMap<>();
    60         map.put("字段名","具体的值");
    61         String tableName="表名";
    62         System.out.println(selectQuerySQL(map,tableName).toString());
    63         //调用insert的方法
    64         Map<String,Object> map=new HashMap<>();
    65         map.put("字段名","具体的值");
    66         map.put("tableName","这里最后添加表名");
    67         String sql = insertSQL(map);
    68         PreparedStatement statment = connection.prepareStatement(sql);
    69         statment.execute(sql);//这里是执行SQL语句,需要借助JDBCUtil中的Connection得到connection
    70     }*/
    71 }

     以上添加的方法有点小问题,改进一下:

      // 添加的公共方法
        public static String insertSQL(Map<String, Object> map) {
            map.put("tableName", "huawei_data_after_conversionu");
            Iterator<String> iterator = map.keySet().iterator();
            StringBuilder sqlTable = new StringBuilder("insert into ");
            StringBuilder sqlKey = new StringBuilder("(");
            StringBuilder sqlVaule = new StringBuilder("value(");
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (map.get(key).toString() == null || map.get(key).toString().length() == 0) {
                    iterator.remove();
                } else {
                    if (key.equals("tableName")) {
                        sqlTable.append(map.get(key));
                    } else {
                        sqlKey.append(key + ",");
                        if (map.get(key) instanceof String) {
                            sqlVaule.append("'" + map.get(key) + "',");
                        } else {
                            sqlVaule.append(map.get(key) + ",");
                        }
    
                    }
                }
            }
            sqlTable.append(sqlKey.toString().substring(0, sqlKey.toString().lastIndexOf(",")) + ")")
                    .append(sqlVaule.toString().substring(0, sqlVaule.lastIndexOf(",")) + ")");
            String sql = sqlTable.toString();
            System.out.println("待执行的insert SQL:" + sql);
            return sql;
        }
    
        // 执行SQL方法
        @SuppressWarnings({ "static-access", "unused" })
        public void executeSql(Connection connection, Statement statement, String sql) throws SQLException {
            try {
                connection = jdbcUtil.getConnection();
                statement = connection.createStatement();
                if (connection == null) {
                    return;
                }
                int count = statement.executeUpdate(sql);// 这里是执行SQL语句插入
                if (count > 0) {
                    logger.info("实时数据插入成功");
                } else {
                    logger.info("实时数据插入失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("发生异常情况,导致插入失败,请重试...........");
            } finally {
                jdbcUtil.CloseConnection(connection, statement, null);
            }
        }
  • 相关阅读:
    一个老鸟发的公司内部整理的 Android 学习路线图
    Android studio button 按钮 四种绑定事件的方法
    Android 运行时权限处理(from jianshu)
    Android 运行时权限处理
    Android studio 快捷键
    Android Toast:是一个类,主要管理消息的提示
    Android开发使用软件
    重装系统,打开VS进行程序调试运行的时候 Unable to find manifest signing certificate in the certificate store
    dev gridview指定单元格cell获取坐标
    IOS 杂笔-3 (property参数)
  • 原文地址:https://www.cnblogs.com/wangquanyi/p/11329035.html
Copyright © 2020-2023  润新知