0

我有一个更新 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 中处理这个问题?

4

4 回答 4

2

您可以尝试如下静态查询吗?

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

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

于 2012-10-17T03:04:32.410 回答
1

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

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

于 2012-10-17T02:47:11.857 回答
1

使用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);
于 2012-10-17T03:11:43.483 回答
0

请看以下代码:

       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);
于 2012-10-17T02:50:51.173 回答