【问题标题】:Selective update using Prepared Statement使用 Prepared Statement 进行选择性更新
【发布时间】:2012-10-17 02:36:25
【问题描述】:

我有一个更新 MySQL 表的函数。我需要根据参数是否存在对字段进行选择性更新。这就是我现在编码的方式。

        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);

当我这样做时,如何确定参数索引?根据存在的参数(如果条件),参数索引也会有所不同,对吧?我该如何解决这个问题?有没有更好的方法在 Java 中处理这个问题?

【问题讨论】:

    标签: java mysql prepared-statement


    【解决方案1】:

    你能试试下面的静态查询吗?

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

    从概念上讲,如果它是0null,我建议使用它自己更新列。这样,您就不需要创建动态查询字符串或动态设置参数。

    【讨论】:

    • 给我语法错误 - “您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在 ') 附近使用正确的语法,status=IFNULL("跨度>
    • 在没有employerid 的情况下尝试一次。由于两个“?”,我有点怀疑。我在 Oracle 中使用 decode 尝试了类似的东西,效果很好。
    【解决方案2】:

    SqlBuilder 库提供了一种在 Java 程序中生成动态 SQL 查询的更好方法。 QueryPreparer 类通过为您跟踪索引来专门解决您在此处遇到的问题。

    免责声明,我是 SqlBuilder 项目的主要开发者。

    【讨论】:

    • 谢谢。我会为下一个项目保留这个。我不是 Java 专家,因此研究任何新库都需要时间。 +1 了
    【解决方案3】:

    使用 arraylist 怎么样?当你第一次检查参数是否存在时,添加到arraylist。之后,迭代数组列表并设置参数。在这种情况下,

    它不仅决定了参数索引,而且还避免了 检查参数再次存在。

    这样

    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);
    

    【讨论】:

      【解决方案4】:

      请看以下代码:

             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);
      

      【讨论】:

        猜你喜欢
        • 2015-01-16
        • 1970-01-01
        • 1970-01-01
        • 2016-10-29
        • 2020-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多