使用带有JDBC的if语句条件更新数据库

时间:2015-07-20 11:11:30

标签: java database if-statement jdbc

您好我正在尝试实现另一个名为afterMigration的更新方法,以检查其余列是否未通过添加一些条件来更新以手动更新。我不确定如何将resultset传递给afterMigration方法

If statements implement in afterMigration method:
If traffic_profile_id 1-24 than cos_profile_type =LEGACY, pe_ingress_flag =T, pe_egress_flag = T, ce_ingress_flag =F, ce_egress_flag = F.
If traffic_profile_id 127-137 than cos_profile_type =STANDARD, pe_ingress_flag =T, pe_egress_flag = T, ce_ingress_flag =F, ce_egress_flag = F.
If traffic_profile_id 800-899 than cos_profile_type =STANDARD, pe_ingress_flag =T, pe_egress_flag = T, ce_ingress_flag =F, ce_egress_flag = F.
if cosModel == 'optB' than cos_profile_type = 'optionB',pe_ingress_flag =T, pe_egress_flag = T, ce_ingress_flag =F, ce_egress_flag = F.

确保标志为空首先如果标志已经设置,我不应该更新记录。

public ResultSet selectRecordsIcore(Connection dbConnection) throws SQLException, ClassNotFoundException {


    Statement statement = null;

    String selectTableSQL = "SELECT profile_id, ingress_flag, egress_flag, ce_ingress_flag, ce_egress_flag, profile_ind from COS_PROFILE"
            + " WHERE profile_id >= ? AND profile_id <= ? ;";

    System.out.println("ICORE Select Statement: " +  selectTableSQL);

    //Gets the max profile_id record
    statement = dbConnection.createStatement();
    ResultSet r = statement.executeQuery("SELECT max(profile_id) AS rowcount FROM COS_PROFILE");       
    r.next();
    int maxCount = r.getInt("rowcount");
    r.close();
    statement.close();
    System.out.println("COS_PROFILE table has max profile_id of " + maxCount );

    preparedStatement = dbConnection.prepareStatement(selectTableSQL);
    preparedStatement.setInt(1, 1);
    preparedStatement.setInt(2, maxCount);

    // execute select SQL statement
    ResultSet rs = preparedStatement.executeQuery();

    return rs;


}

private void updateRecordIntoBids(Connection dbConnection, ResultSet rs) throws Exception {

    PreparedStatement preparedStatement = null;

    String updateTableSQL = 
            "UPDATE traffic_profile_temp SET pe_ingress_flag  = ?, "
                    + " pe_egress_flag = ?,"
                    + " ce_ingress_flag = ?,"
                    + " ce_egress_flag = ?, "
                    + " cos_profile_type = ? "
                    + " WHERE traffic_profile_id = ?  ";

    // execute update SQL statement
    System.out.println("BIDS Update Statement: " + updateTableSQL);

    preparedStatement = dbConnection.prepareStatement(updateTableSQL);
    System.out.println("Updating Bids Database in process ...");

    int rowCount = 0;   
    int expectedId = 1;
    int first = 0;

    while (rs.next()) {

        String ingressflag = rs.getString("ingress_flag");     //BIDS column is pe_ingress_flag
        String egressflag = rs.getString("egress_flag");       //BIDS column is pe_egress_flag
        String ceingressflag = rs.getString("ce_ingress_flag"); //BIDS column is ce_ingress_flag
        String ceegressflag = rs.getString("ce_egress_flag");  //BIDS column is ce_egress_flag
        String profileind = translate(rs.getString("profile_ind"));       //BIDS column is cos_profile_type
        int profileid = rs.getInt("profile_id");                //BIDS column is traffic_profile_id

        preparedStatement.setString(1, ingressflag);
        preparedStatement.setString(2, egressflag);
        preparedStatement.setString(3, ceingressflag);
        preparedStatement.setString(4, ceegressflag);
        preparedStatement.setString(5, profileind);
        preparedStatement.setInt(6, profileid);
        preparedStatement.addBatch();
        rowCount++;


        if(expectedId == profileid){
            if(first == 0){
                first = expectedId-1;
            }

        }

        if(expectedId != profileid){
            if(first > 0){
                System.out.println ("Profile id "+first+" to "+(expectedId-1)+" update.");
                first = 0;
            }
            System.out.println ("Profile id "+expectedId+" to "+(profileid-1)+" missing.");
            expectedId = profileid;
        }

        expectedId++;
    }
    if(first > 0){
        System.out.println("Profile id "+first+" to "+(expectedId-1)+" update.");
        first = 0;
    }
    System.out.println("Executing batch update for profile_id " +first+" to "+(expectedId-1));
    preparedStatement.executeBatch();
    System.out.println("Completed batch update");
    dbConnection.commit();
    System.out.println("Commit Completed");

    System.out.println("Number of total update statements in batch " + rowCount  );


    if (preparedStatement != null) {
        preparedStatement.close();
        System.out.println("BIDS PreparedStatement Connection is closed");
    }

}

这是我开始的psedo代码:

  public static  String afterMigration(String indicator) throws Exception {

    String selectTableSQL = "SELECT traffic_profile_id from TRAFFIC_PROFILE"
            + " WHERE pe_egress_flag IS NULL "
            + "   AND ce_ingress_flag IS NULL "
            + "   AND ce_ingress_flag IS NULL "
            + "   AND ce_egress_flag IS NULL "
            + "   AND cos_profile_type NULL "
            + "   AND traffic_profile_id >= ? AND traffic_profile_id <= ? ;";


    while (rs.next()){

    }
    String ind;
    ind = indicator;

    if (indicator != null) {
        return indicator;
    }

    else if ((indicator == null || indicator.length() == 0 ) && (trafficprofileid <= 24 )){
        String updateTableSQL1 = 
                "UPDATE traffic_profile_temp SET pe_ingress_flag  = 'T', "
                        + " pe_egress_flag = 'T',"
                        + " ce_ingress_flag = 'F',"
                        + " ce_egress_flag = 'F', "
                        + " cos_profile_type = 'LEGACY' "
                        + " WHERE traffic_profile_id >= 24    ";
        return  //set profileind = "LEGACY"
                //set ingressflag = "T"
                //set ingressflag = "T"
                //set ceingressflag = "F"
                //set ceegressflag  = "F"}

    else if ((indicator == null || indicator.length() == 0 ) && (trafficprofileid >= 127 &&  trafficprofileid <= 137)){
        return  //set profileind = "STANDARD"
                //set ingressflag = "T"
                //set ingressflag = "T"
                //set ceingressflag = "F"
                //set ceegressflag  = "F"}

    else if ((indicator == null || indicator.length() == 0 ) && (trafficprofileid >= 800 &&  trafficprofileid <= 899)){
        return  //set profileind = "STANDARD"
                //set ingressflag = "T"
                //set ingressflag = "T"
                //set ceingressflag = "F"
                //set ceegressflag  = "F"}

        else if (cosModel.equals("optB")){
            return //set profileind = OPTIONB
                    //set ingressflag = "T"
                    //set ingressflag = "T"
                    //set ceingressflag = "F"
                    //set ceegressflag  = "F";
        }
    return ind;
}

0 个答案:

没有答案