从下面的 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;
}
公共无效执行更新(){
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();
}
}
}