【问题标题】:Checking the condition Before uploading in to the table在上传到表格之前检查条件
【发布时间】:2013-08-19 10:06:32
【问题描述】:

通过下面的 executeUpdate() 方法,我想更新 db2 表中的数据。在此之前,我想检查 C_Conf 和 D_Conf 是否具有值“是”意味着我需要替换为“Y”,如果值为“否”则意味着我需要替换为“N”。我可以在哪里检查并附加到更新查询。

在 executeUpdate() 中,我只是将 C_Conf 和 D_Conf 的值硬编码为“N”。在这里我想检查值是否为“是”意味着我需要替换为“Y”,或者如果它是“否”意味着我需要替换为“N”。如何检查以及在代码中的位置?请帮忙

 public class DbTask {

Connection connection;

Statement statement, statement1; 

public boolean executeQuery(String dbQuery){

    boolean  result = false;

     connection = DatabaseConnection.getCon();

     try {

         statement = connection.createStatement();


                     result = statement.execute(dbQuery);

    } catch (SQLException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

   return result;
}


public boolean cleanTable(String schema, String tableName) {

    boolean  result = false;

     connection = DatabaseConnection.getCon();

     try {

         statement = connection.createStatement();

        // can be implemented later for deleteing the table data

    } catch (SQLException e) {


        // TODO Auto-generated catch block
        e.printStackTrace();
    }

   return result;

}

public void executeUpdate() {

  String selectQuery = "select 

                                                                                                                 S_NUMBER,CON,D_CON,R_CON,VAL_CON 

                      from

                      OTG.S_SAMPLE_VAL" 

                      +"  WHERE R_TS = (SELECT MAX(R_TS) FROM 

                       OTG.S_SAMPLE_VAL)";

  Statement statement;

try {
     connection = DatabaseConnection.getCon();

     statement = connection.createStatement();

     statement1 = connection.createStatement();

  ResultSet rs = statement.executeQuery(selectQuery);

  while(rs.next()){

      StringBuffer updateQuery = new StringBuffer();

       updateQuery.append("update OTG.R_VAL set ");

       updateQuery.append("C_Conf='");

       updateQuery.append( "N', ");

      // updateQuery.append(rs.getString(2) + "', ");

       updateQuery.append("D_Conf='");

     //  updateQuery.append(rs.getString(3) + "', ");

                updateQuery.append( "N', ");

       updateQuery.append("REVE=");

       updateQuery.append(rs.getString(4) + ", ");

       updateQuery.append("VAL='");

       updateQuery.append(rs.getString(5) + "' ");

       updateQuery.append("where S_NO ='" + rs.getString(1) + "'");

       System.out.println(updateQuery.toString());

       statement1.executeUpdate(updateQuery.toString());

      }



} catch (SQLException e) {

    // TODO Auto-generated catch block

          e.printStackTrace();

}

}

}

【问题讨论】:

标签: java jdbc db2


【解决方案1】:

您为什么不只更新单个查询中的值,而不是先找到行然后更新它。让数据库找到并返回该行没有多大意义,只是在另一个(不必要的)数据库之旅中更新它:

update OTG.S_SAMPLE_VAL
   set  C_CONF = case C_CONF when 'Yes' then 'Y'
                             when 'No'  then 'N' 
                             else C_CONF end 
       ,D_CONF = case D_CONF when 'Yes' then 'Y'
                             when 'No'  then 'N' 
                             else D_CONF end    
 where R_TS = (SELECT MAX(R_TS) FROM OTG.S_SAMPLE_VAL)
   and C_CONF in ('Yes','No') or D_CONF in ('Yes','No');

更好的是,为什么不在数据库中添加触发器以在插入发生之前更新行,或者更好的是,更新正在插入行的应用程序以插入所需的值(@987654322 @ 或 'N') ?

【讨论】:

  • 嗨,Lan,感谢您的回复,我正在通过从表 S_SAMPLE_VAL 中选择与列 S_NO 相关的不同行来更新表 R_VAL 中的数据。在将数据更新到表 R_VAL 时,我需要替换列值购买“Y”如果它是“是”或“N”如果它是“否”,无论使用的情况(大写还是小写)。
  • 我尝试了上面的查询.. 但它导致错误为“从只允许一列的子查询返回多列.. SQLCODE=-412, SQLSTATE=42823, DRIVER=4.8.86 ” 。正如你所说,我尝试了以下方法。
  • 更新 OTG.R_VAL 设置 C_CONF = case C_CONF when 'Yes' then 'Y' when 'No' then 'N' else C_CONF end ,D_CONF = case D_CONF when 'Yes' then 'Y' when 'No' then 'N' else D_CONF end where S_NO = (SELECT S_NUM,CON,DIS,REV,VAL FROM OTG.S_R_VAL WHERE R_TS = (SELECT MAX(R_TS) FROM OTG.S_R_VAL)) 和 C_CONF in ('Yes' ,'No') 或 C_CONF in ('Yes','No')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 2013-01-28
  • 2018-05-22
  • 1970-01-01
  • 2020-02-16
  • 2023-04-01
  • 2012-06-09
相关资源
最近更新 更多