Combining two methods into one

F

francan00

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

......
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?


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.setString(3,mybean.getMainCity());
prep.setString(4,mybean.getMainState());
prep.executeUpdate();
}
catch(Exception e) {
e.printStacktrace();
}
return status;
}

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


Please advise.
 
M

Matt Humphrey

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

<snip code>

Your combined insert will definately fail because the table has only 2
fields but you are attempting to assign 4 fields. Fields 3 and 4 don't
exist. You original code shows that the insertion should create 2 rows--two
distinct insertions. You will have to duplicate the executeUpdate() portion
to achieve that. Something like:

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

I'm just copying your words--I have no idea what your "prep" object is or
how it works.

Matt Humphrey http://www.iviz.com/
 
F

francan00

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

<snip code>

Your combined insert will definately fail because the table has only 2
fields but you are attempting to assign 4 fields. Fields 3 and 4 don't
exist. You original code shows that the insertion should create 2 rows--two
distinct insertions. You will have to duplicate the executeUpdate() portion
to achieve that. Something like:

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

I'm just copying your words--I have no idea what your "prep" object is or
how it works.

Matt Humphreyhttp://www.iviz.com/

Thanks,

How would I call the method where the method would know what to
execute? Would I just call it like this?
inserterCombined(mybean);
 
M

Matt Humphrey

| >
| > | > |I have a 2 methods working in my class file that I am trying to
| > | convert into 1 method:
| > |
| >
| > <snip code>
| >
| > Your combined insert will definately fail because the table has only 2
| > fields but you are attempting to assign 4 fields. Fields 3 and 4 don't
| > exist. You original code shows that the insertion should create 2
rows--two
| > distinct insertions. You will have to duplicate the executeUpdate()
portion
| > to achieve that. Something like:
| >
| > 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();
| >
| > I'm just copying your words--I have no idea what your "prep" object is
or
| > how it works.
| >
| > Matt Humphreyhttp://www.iviz.com/
|
| Thanks,
|
| How would I call the method where the method would know what to
| execute? Would I just call it like this?
| inserterCombined(mybean);

I would think so, although doing so assumes that the bean is fully defined
for City/State, MainCity/MainState.
 
M

mark.donaghue

| >
| >| > |I have a 2 methods working in my class file that I am trying to
| > | convert into 1 method:
| > |
| >
| > <snip code>
| >
| > Your combined insert will definately fail because the table has only 2
| > fields but you are attempting to assign 4 fields. Fields 3 and 4 don't
| > exist. You original code shows that the insertion should create 2
rows--two
| > distinct insertions. You will have to duplicate the executeUpdate()
portion
| > to achieve that. Something like:
| >
| > 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();
| >
| > I'm just copying your words--I have no idea what your "prep" object is
or
| > how it works.
| >
| > Matt Humphreyhttp://www.iviz.com/
|
| Thanks,
|
| How would I call the method where the method would know what to
| execute? Would I just call it like this?
| inserterCombined(mybean);

I would think so, although doing so assumes that the bean is fully defined
for City/State, MainCity/MainState.

How about a method like this:

public int inserterOne(String city, String State)

Call it the first time for city/state, the second time with mainCity/
mainState.
 
E

EricF

How about a method like this:

public int inserterOne(String city, String State)

Call it the first time for city/state, the second time with mainCity/
mainState.
Maybe that doesn't fit his homework assignment?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top