检查条件在上传到表格之前

时间:2013-08-19 10:06:33

标签: java jdbc db2

从下面的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();

}

}

}

1 个答案:

答案 0 :(得分: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');

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