Static or object for db insert

Discussion in 'Java' started by teser3@hotmail.com, Sep 27, 2008.

  1. Guest

    For the past year I been using Database insert method where I create
    an object in my servlet when inserting into my Oracle 9i database.
    The insertData method is using PreparedStatement. My insertData method
    has closing statements in finally block. There are about 5 records
    input in the database per day.

    PeopleServlet:

    if(myCondition == 0){
    new PeopleInfo().insertData(personObject);
    }

    I was wondering about using static insertData method instead of
    creating object. Please advise what the negatives are for the way I am
    doing it compared to using a static method to insert database info.
    , Sep 27, 2008
    #1
    1. Advertising

  2. Guest

    On Sep 27, 9:17 am, Lew <> wrote:
    > wrote:
    > > For the past year I been using Database insert method where I create
    > > an object in my servlet when inserting into my Oracle 9i database.
    > > The insertData method is using PreparedStatement. My insertData method
    > > has closing statements in finally block. There are about 5 records
    > > input in the database per day.

    >
    > > PeopleServlet:

    >
    > > if(myCondition == 0){
    > >     new PeopleInfo().insertData(personObject);
    > > }

    >
    > > I was wondering about using static insertData method instead of
    > > creating object. Please advise what the negatives are for the way I am
    > > doing it compared to using a static method to insert database info.

    >
    > If you aren't willing to create an object, then you have to manage all the
    > individual items piecemeal, and explicitly, in the calling code.  If
    > 'PeopleInfo' is a fairly lightweight holder for those items, the weight of a
    > holder object is barely more, or perhaps no more than the weight of the
    > contained items.  The code would be perhaps less encapsulated and extensible.
    > There don't seem to be many advantages, if any, of the static method approach.
    >   The instance approach makes for readable, encapsulated code.
    >
    > What do you fancy will improve if you use a static method?
    >
    > --
    > Lew- Hide quoted text -
    >
    > - Show quoted text -


    Thanks Lew, your always very helpful for my Java learning!

    I thought static method would be better performance and more accepted
    practice in Java because it uses less JVM? And yes PeopleInfo is very
    lightweight with few conditions and it (Servlet controller) is part of
    my MVC pattern working on Tomcat.
    , Sep 27, 2008
    #2
    1. Advertising

  3. Guest

    On Sep 27, 9:17 am, Lew <> wrote:
    > wrote:
    > > For the past year I been using Database insert method where I create
    > > an object in my servlet when inserting into my Oracle 9i database.
    > > The insertData method is using PreparedStatement. My insertData method
    > > has closing statements in finally block. There are about 5 records
    > > input in the database per day.

    >
    > > PeopleServlet:

    >
    > > if(myCondition == 0){
    > >     new PeopleInfo().insertData(personObject);
    > > }

    >
    > > I was wondering about using static insertData method instead of
    > > creating object. Please advise what the negatives are for the way I am
    > > doing it compared to using a static method to insert database info.

    >
    > If you aren't willing to create an object, then you have to manage all the
    > individual items piecemeal, and explicitly, in the calling code.  If
    > 'PeopleInfo' is a fairly lightweight holder for those items, the weight of a
    > holder object is barely more, or perhaps no more than the weight of the
    > contained items.  The code would be perhaps less encapsulated and extensible.
    > There don't seem to be many advantages, if any, of the static method approach.
    >   The instance approach makes for readable, encapsulated code.
    >
    > What do you fancy will improve if you use a static method?
    >
    > --
    > Lew- Hide quoted text -
    >
    > - Show quoted text -


    Thanks Lew, your always very helpful for my Java work!

    I thought static method would be better performance and more accepted
    practice in Java because it uses less JVM? And yes PeopleInfo is
    very
    lightweight with few conditions and it (Servlet controller) is part
    of
    my MVC pattern working on Tomcat.
    , Sep 27, 2008
    #3
  4. Guest

    On Sep 28, 12:35 am, Lew <> wrote:
    > wrote:
    > > I thought static method would be better performance and more accepted
    > > practice in Java because it uses less JVM?  

    >
    > It doesn't use "less JVM" to use a static method, necessarily, and static
    > methods are no more "accepted practice" than instance methods.
    >
    > There are use cases for static methods, of course.  The best practice is to do
    > the right thing for the algorithm at hand; you cannot simply say that static
    > methods are better than instance methods or vice versa.
    >
    > Most of the time instance methods are better.  That's because most of the time
    > you want actions to be controlled through an object, and not globally by a
    > class.  The object that owns an instance method can carry state and control
    > its behavior independently of other instances.  Concurrency is usually easier
    > to handle with instance methods also.
    >
    > Notice the words "most of the time", "can carry", "usually easier".  When in
    > doubt, you probably want an instance method, but when behavior must inhere at
    > the class level then you must use a static method.
    >
    > Global utility methods, such as the static Math functions (min(), cos(),
    > etc.), are good candidates for static methods.  Factory methods, those that
    > actually create class instances, will usually be static.  Class-wide
    > behaviors, like registering instances with a class-level registry, have to be
    > static.  When the method has to be static, you should have no doubt.
    > Otherwise suspect that the method should be instance-level.
    >
    > > And yes PeopleInfo is very
    > > lightweight with few conditions and it (Servlet controller) is part
    > > of
    > > my MVC pattern working on Tomcat.

    >
    > I think in your particular case that you should stay with an instance method.
    >   The PeopleInfo instance controls it nicely and holds related state in a way
    > that static variables probably would do less well.  Without an SSCCE it's a
    > little hard to say for sure, so analyze thoroughly.
    >
    > --
    > Lew


    Very helpful information. Thanks for all your time and knowledge.
    , Sep 28, 2008
    #4
  5. Arne Vajhøj Guest

    wrote:
    >> wrote:
    >>> For the past year I been using Database insert method where I create
    >>> an object in my servlet when inserting into my Oracle 9i database.
    >>> The insertData method is using PreparedStatement. My insertData method
    >>> has closing statements in finally block. There are about 5 records
    >>> input in the database per day.
    >>> PeopleServlet:
    >>> if(myCondition == 0){
    >>> new PeopleInfo().insertData(personObject);
    >>> }
    >>> I was wondering about using static insertData method instead of
    >>> creating object. Please advise what the negatives are for the way I am
    >>> doing it compared to using a static method to insert database info.


    > I thought static method would be better performance and more accepted
    > practice in Java because it uses less JVM? And yes PeopleInfo is
    > very
    > lightweight with few conditions and it (Servlet controller) is part
    > of
    > my MVC pattern working on Tomcat.


    Performance is not very relevant with 5 inserts per day.

    Static methods is only accepted in very few cases in Java.

    As a general rule: if in doubt between static and non-static
    always chose non-static.

    I don't think I have ever had to change a non-static method
    to a static method.

    I will need a pretty big int to calculate the number of times
    I have had to go the other way.

    Arne
    Arne Vajhøj, Oct 5, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cannontrodder
    Replies:
    1
    Views:
    708
    cannontrodder
    Jul 25, 2006
  2. tropos
    Replies:
    3
    Views:
    448
  3. Replies:
    1
    Views:
    433
  4. Replies:
    1
    Views:
    1,766
    Albert Hopkins
    Dec 6, 2008
  5. eagle

    Insert & Insert/Repeat buttons in a detailsview

    eagle, Jul 12, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    1,033
    eagle
    Jul 12, 2007
Loading...

Share This Page