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.
Matt Humphrey - 08 Oct 2007 14:18 GMT
|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/
francan00@yahoo.com - 08 Oct 2007 14:41 GMT
> <franca...@yahoo.com> wrote in message
>
[quoted text clipped - 24 lines]
>
> 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);
Matt Humphrey - 08 Oct 2007 16:39 GMT
| > <franca...@yahoo.com> wrote in message
| >
[quoted text clipped - 30 lines]
| 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.
mark.donaghue@gmail.com - 08 Oct 2007 22:34 GMT
> <franca...@yahoo.com> wrote in message
>
[quoted text clipped - 40 lines]
> 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.
EricF - 09 Oct 2007 04:27 GMT
>> <franca...@yahoo.com> wrote in message
>>
[quoted text clipped - 48 lines]
>Call it the first time for city/state, the second time with mainCity/
>mainState.
Maybe that doesn't fit his homework assignment?