Combine methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oaklander
    New Member
    • Aug 2007
    • 20

    #1

    Combine methods

    I have a 2 methods working in my class file that I am trying to convert into 1 method:

    Code:
    .....
    public Preparestatement prep;
    public int inserterOne(TheBean mybean)
    {
        int status = 0;
        try {
        prep.connection.preparestatement("insert into person (city, state) values (?,?)");
        prep.setString(1,mybean.getCity());
        prep.setString(2,mybean.getState());
        prep.executeUpdate();
        }
        catch(Exception e) {
             e.printStacktrace();
        }
        return status;
     }
     
    public int inserterTwo(TheBean mybean)
    {
        int status = 0;
        try {
        prep.connection.preparestatement("insert into person (city, state) values (?,?)");
        prep.setString(1,mybean.getMainCity());
        prep.setString(2,mybean.getMainState());
        prep.executeUpdate();
        }
        catch(Exception e) {
             e.printStacktrace();
        }
        return status;
     }
     
    public int hitter(TheBean mybean)
    {
    .....
    inserterOne(mybean);
    inserterTwo(mybean);    
    ...


    Here is my attempt and not sure how to make this work?
    Code:
    public int inserterCombined(TheBean mybean, ??)
    {
        int status = 0;
        try {
        prep.connection.preparestatement("insert into person (city, state) values (?,?)");
        prep.setString(1,mybean.getCity());
        prep.setString(2,mybean.getState());
        prep.executeUpdate();
    
        prep.setString(1,mybean.getMainCity());
        prep.setString(2,mybean.getMainState());
        prep.executeUpdate();
        }
        catch(Exception e) {
             e.printStacktrace();
        }
        return status;
     }
     
    ....
    public int hitter(TheBean mybean)
    {
    .....
    inserterCombined(?? here);
    Please advise.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Why would you want to "combine" methods?
    Do they do the same thing or do you want to call one method after the other?
    What happened on your attempt? Did you get errors/exceptions?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by oaklander
      I have a 2 methods working in my class file that I am trying to convert into 1 method:
      Don't; go the other way instead and split them up even further (folks call that
      'refactoring'). Suppose you have two methods:

      M1: a B c
      M2: a' B c'

      where B is the common part of them both and a, a', c and c' are the unique
      prefixes and postfixes of both methods. Design a third method M3 such that
      you can rewrite your methods M1 and M2 like this:

      M1: a M3 c
      M2: a' M3 c'
      M3: B

      More methods are nothing to be ashamed of, the opposite is.

      kind regards,

      Jos

      Comment

      Working...