Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine methods 1

Status
Not open for further replies.

evergrean100

Technical User
Dec 1, 2006
115
US
I have 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);  //how would I know which part of the method to call here?

Please advise how I can get this to work. Thanks.
 
Why not have inserterOne call inserterTwo?

_________________
Bob Rashkin
 
The two methods are so similar that we can use one method instead.

I prefer flexible method.
Code:
public int inserterOne(String input1, String input2)
{
    int status = 0;
    try {
    prep.connection.preparestatement("insert into person (city, state) values (?,?)");
    prep.setString(1,mybean.input1);
    prep.setString(2,mybean.input2);
    prep.executeUpdate();
    }
    catch(Exception e) {
         e.printStacktrace();
    }
    return status;
 }

public int hitter(TheBean mybean)
{
 ...
 insertOne(mybean.getCity(),mybean.getState());
 insertOne(mybean.getMainCity(),mybean.getMainState());
 

Code:
public int hitter(TheBean mybean)
{
 ...
 inserterOne(mybean.getCity(),mybean.getState());
 inserterOne(mybean.getMainCity(),mybean.getMainState());
 
sorry for two times of mistakes
Code:
public int inserterOne(String input1, String input2)
{
    int status = 0;
    try {
    prep.connection.preparestatement("insert into person (city, state) values (?,?)");
    prep.setString(1,input1);
    prep.setString(2,input2);
    prep.executeUpdate();
    }
    catch(Exception e) {
         e.printStacktrace();
    }
    return status;
 }

public int hitter(TheBean mybean)
{
 ...
 inserterOne(mybean.getCity(),mybean.getState());
 inserterOne(mybean.getMainCity(),mybean.getMainState());
...
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top