Welcome to Dream.In.Code
Become a Java Expert!

Join 149,581 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,758 people online right now. Registration is fast and FREE... Join Now!




Combine methods

 
Reply to this topicStart new topic

Combine methods

kensington
8 Oct, 2007 - 10:09 AM
Post #1

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 31


My Contributions
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
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Combine Methods
8 Oct, 2007 - 11:28 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Well not sure what your trying to do here, so please explain
User is offlineProfile CardPM
+Quote Post

kensington
RE: Combine Methods
8 Oct, 2007 - 01:08 PM
Post #3

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 31


My Contributions
QUOTE(PennyBoki @ 8 Oct, 2007 - 12:28 PM) *

Well not sure what your trying to do here, so please explain


Thanks for your reply.

Each one of the two methods has a prep statement insert and I call each method in a third method:
CODE

public int hitter(TheBean mybean)
{
.....
inserterOne(mybean);
inserterTwo(mybean);    
...


I was hoping to combine the two methods into one and just call one method:
CODE

public int hitter(TheBean mybean)
{
.....
inserterCombined(mybean);
...


Cant seem to figure out how I would do it or is this the best way to combine it in the inserterCombined method:
CODE

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();



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:45PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month