简体   繁体   English

使用预准备语句的选择性更新

[英]Selective update using Prepared Statement

I have one function which updates a MySQL table. 我有一个更新MySQL表的函数。 I need to do a selective update of fields according to whether the parameters are present or not. 我需要根据参数是否存在对字段进行选择性更新。 This is how I have coded it right now. 这就是我现在编码的方式。

        String sql =  "";
        if (employerId > 0) sql = sql + "employerid=?,";
        if (status != null) sql += " status=?,";
        if(printurl != null) sql += " url=?";
        if (sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
        PreparedStatement ps = con.prepareStatement("update employer set "
                + sql
                + "where id=?");

       if (employerId > 0) ps.setInt(1, employerId);
       if (status != null) ps.setString(2,status);

When I do like this, how do I determine the parameter index? 当我这样做时,如何确定参数索引? According to the parameters present (if conditions), the parameter index will also vary, right? 根据存在的参数(如果有条件),参数索引也会有所变化,对吧? How do I get around with this? 我该如何解决? Is there a better way to handle this in Java? 有没有更好的方法来用Java处理呢?

Can you please try a static query as below? 您可以尝试以下静态查询吗?

String sql =  "update employer set employerid=IF(?=0,employerid,?),"+
              "status=IFNULL(?,status), url=IFNULL(?,url) " +
              "where id=?";

Conceptually, I am suggesting to update the column with itself if it is 0 or null . 从概念上讲,如果列为0null ,我建议使用自身更新该列。 This way, you don't need to create dynamic query string or dynamically set the parameters. 这样,您无需创建动态查询字符串或动态设置参数。

The SqlBuilder library provides a much better way to do dynamic SQL query generation in java programs. SqlBuilder库提供了一种在Java程序中进行动态SQL查询生成的更好的方法。 The QueryPreparer class specifically addresses the problem you are encountering here by tracking the indexes for you. QueryPreparer类通过为您跟踪索引来专门解决您在此处遇到的问题。

disclaimer, i am the primary developer of the SqlBuilder project. 免责声明,我是SqlBuilder项目的主要开发人员。

How about using arraylist ? 使用arraylist怎么样? When you first checking whether paramaters are existed, add to arraylist. 首次检查参数是否存在时,将其添加到arraylist。 After that, iterating array list and set parameters. 之后,迭代数组列表并设置参数。 In this case, 在这种情况下,

it determines not only the parameter index but also avoids for checking parameter existed again. 它不仅确定参数索引,而且避免检查参数是否再次存在。

Like that 像那样

List paramList = new ArrayList<Object>();

         if (employerId > 0) {
             sql = sql + "employerid=? ,";
             paramList.add(employerId);
         }
         if (status != null) { 
             sql += " status=? ,";
             paramList.add(status);
         }
         if(printurl != null) {
             sql += " url=? ";
             paramList.add(printurl);
         }
         if (sql.endsWith(",")) 
             sql = sql.substring(0, sql.length()-1);

         PreparedStatement ps = con.prepareStatement("update employer set " + sql + "where id=?");

         for(int i=0; i< paramList.size(); i++) {
             if(paramList.get(i) instanceof Integer) {
                 ps.setInt((i + 1), (Integer)paramList.get(i));
             }
             else if(paramList.get(i) instanceof String) {
                 ps.setString((i + 1), (String)paramList.get(i));
             }
         }
         System.out.println(ps);

please see the following code: 请参见以下代码:

       String sql =  "update employer set ";
        boolean hasParam = false;
        if (employerId > 0){
                sql += " employerid=? ";
                hasParam = true;
        }
        if (status != null){
            if(hasParam){
                sql += " , ";
            }
            sql += " status=? ";
            hasParam = true;
        }
        if(printurl != null){
            if(hasParam){
                sql += " , ";
            }
            sql += " url=?";
        }
        sql += " where id=?";
        PreparedStatement ps = con.prepareStatement(sql);

       int index = 1;
       if(employerId > 0){
           ps.setInt(index++, employerId);
       }
       if(status != null){
           ps.setString(index++, status);
       }
       if(printurl != null){
           ps.setString(index++, printurl);
       }
       ps.setInt(index, id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM