Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Combining two methods into one

Thread view: 
francan00@yahoo.com - 08 Oct 2007 04:31 GMT
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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.