private Constraint createNewConstraint(String name) throws SQLException {
  ResultSet rs=stmtdb2.executeQuery("select Constraint_Type, Table_Name," + "Search_Condition, R_Constraint_Name, Delete_Rule" + " from User_Constraints where Constraint_Name='" + name + "'");
  String tableName="";
  String constraintType="";
  String condition="";
  String rConstraintName="";
  String deleteRule="";
  String column="";
  if (rs.next()) {
    constraintType=rs.getString("Constraint_Type");
    tableName=rs.getString("Table_Name");
    condition=rs.getString("Search_Condition");
    rConstraintName=rs.getString("R_Constraint_Name");
    deleteRule=rs.getString("Delete_Rule");
  }
  rs.close();
  if (constraintType.equals(Constraint.FOREIGN_KEY)) {
    Constraint refConstraint=null;
    rs=stmtdb2.executeQuery("select * from User_Cons_Columns where Constraint_Name='" + name + "'");
    if (rs.next()) {
      column=rs.getString("Column_Name");
    }
    rs.close();
    rs=stmtdb2.executeQuery("select * from User_Cons_Columns where Constraint_Name='" + rConstraintName + "'");
    if (rs.next()) {
      String refColumn=rs.getString("Column_Name");
      String refTableName=rs.getString("Table_Name");
      refConstraint=new Constraint(rConstraintName,refTableName);
      refConstraint.addColumnName(refColumn);
      refConstraint.setDeleteRule(deleteRule);
    }
    rs.close();
    Constraint c=new Constraint(name,tableName);
    c.setType(Constraint.FOREIGN_KEY);
    c.addColumnName(column);
    c.setRConstraint(refConstraint);
    c.addColumnName(column);
    c.setDeleteRule(deleteRule);
    return (c);
  }
 else   if (constraintType.equals(Constraint.PRIMARY_KEY)) {
    rs=stmtdb2.executeQuery("select * from User_Cons_Columns where Constraint_Name='" + name + "'");
    Constraint c=new Constraint(name,tableName);
    c.setType(Constraint.PRIMARY_KEY);
    c.setDeleteRule(deleteRule);
    while (rs.next()) {
      c.addColumnName(rs.getString("Column_Name"));
    }
    rs.close();
    return (c);
  }
 else   if (constraintType.equals(Constraint.CHECK)) {
    rs=stmtdb2.executeQuery("select * from User_Cons_Columns where Constraint_Name='" + name + "'");
    if (rs.next()) {
      column=rs.getString("Column_Name");
    }
    rs.close();
    Constraint c=new Constraint(name,tableName);
    c.setType(Constraint.CHECK);
    c.addColumnName(column);
    c.setCheckCondition(condition);
    c.setDeleteRule(deleteRule);
    return (c);
  }
  return null;
}

private Constraint createNewConstraint ( String name ) throws SQLException { ResultSet rs = stmtdb2 . executeQuery ( "select Constraint_Type, Table_Name," + "Search_Condition, R_Constraint_Name, Delete_Rule" + " from User_Constraints where Constraint_Name='" + name + "'" ) ; String tableName = "" ; String constraintType = "" ; String condition = "" ; String rConstraintName = "" ; String deleteRule = "" ; String column = "" ; if ( rs . next ( ) ) { constraintType = rs . getString ( "Constraint_Type" ) ; tableName = rs . getString ( "Table_Name" ) ; condition = rs . getString ( "Search_Condition" ) ; rConstraintName = rs . getString ( "R_Constraint_Name" ) ; deleteRule = rs . getString ( "Delete_Rule" ) ; } rs . close ( ) ; if ( constraintType . equals ( Constraint . FOREIGN_KEY ) ) { Constraint refConstraint = null ; rs = stmtdb2 . executeQuery ( "select * from User_Cons_Columns where Constraint_Name='" + name + "'" ) ; if ( rs . next ( ) ) { column = rs . getString ( "Column_Name" ) ; } rs . close ( ) ; rs = stmtdb2 . executeQuery ( "select * from User_Cons_Columns where Constraint_Name='" + rConstraintName + "'" ) ; if ( rs . next ( ) ) { String refColumn = rs . getString ( "Column_Name" ) ; String refTableName = rs . getString ( "Table_Name" ) ; refConstraint = new Constraint ( rConstraintName , refTableName ) ; refConstraint . addColumnName ( refColumn ) ; refConstraint . setDeleteRule ( deleteRule ) ; } rs . close ( ) ; Constraint c = new Constraint ( name , tableName ) ; c . setType ( Constraint . FOREIGN_KEY ) ; c . addColumnName ( column ) ; c . setRConstraint ( refConstraint ) ; c . addColumnName ( column ) ; c . setDeleteRule ( deleteRule ) ; return ( c ) ; } else if ( constraintType . equals ( Constraint . PRIMARY_KEY ) ) { rs = stmtdb2 . executeQuery ( "select * from User_Cons_Columns where Constraint_Name='" + name + "'" ) ; Constraint c = new Constraint ( name , tableName ) ; c . setType ( Constraint . PRIMARY_KEY ) ; c . setDeleteRule ( deleteRule ) ; while ( rs . next ( ) ) { c . addColumnName ( rs . getString ( "Column_Name" ) ) ; } rs . close ( ) ; return ( c ) ; } else if ( constraintType . equals ( Constraint . CHECK ) ) { rs = stmtdb2 . executeQuery ( "select * from User_Cons_Columns where Constraint_Name='" + name + "'" ) ; if ( rs . next ( ) ) { column = rs . getString ( "Column_Name" ) ; } rs . close ( ) ; Constraint c = new Constraint ( name , tableName ) ; c . setType ( Constraint . CHECK ) ; c . addColumnName ( column ) ; c . setCheckCondition ( condition ) ; c . setDeleteRule ( deleteRule ) ; return ( c ) ; } return null ; }
creates a new constraint from the data found for the constraint with the given name in db2 .
