Static or object for db insert

  • Follow


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.
0
Reply teser3 (98) 9/27/2008 3:21:31 AM

teser3@hotmail.com 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
0
Reply noone7 (3512) 9/27/2008 1:17:28 PM


On Sep 27, 9:17=A0am, Lew <no...@lewscanon.com> wrote:
> tes...@hotmail.com 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 =3D=3D 0){
> > =A0 =A0 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 th=
e
> individual items piecemeal, and explicitly, in the calling code. =A0If
> 'PeopleInfo' is a fairly lightweight holder for those items, the weight o=
f a
> holder object is barely more, or perhaps no more than the weight of the
> contained items. =A0The code would be perhaps less encapsulated and exten=
sible.
> There don't seem to be many advantages, if any, of the static method appr=
oach.
> =A0 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.
0
Reply teser3 (98) 9/27/2008 5:07:15 PM

On Sep 27, 9:17=A0am, Lew <no...@lewscanon.com> wrote:
> tes...@hotmail.com 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 =3D=3D 0){
> > =A0 =A0 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 th=
e
> individual items piecemeal, and explicitly, in the calling code. =A0If
> 'PeopleInfo' is a fairly lightweight holder for those items, the weight o=
f a
> holder object is barely more, or perhaps no more than the weight of the
> contained items. =A0The code would be perhaps less encapsulated and exten=
sible.
> There don't seem to be many advantages, if any, of the static method appr=
oach.
> =A0 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.
0
Reply teser3 (98) 9/27/2008 5:09:15 PM

teser3@hotmail.com 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
0
Reply noone7 (3512) 9/28/2008 4:35:13 AM

On Sep 28, 12:35=A0am, Lew <no...@lewscanon.com> wrote:
> tes...@hotmail.com wrote:
> > I thought static method would be better performance and more accepted
> > practice in Java because it uses less JVM? =A0
>
> 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. =A0The best practice i=
s to do
> the right thing for the algorithm at hand; you cannot simply say that sta=
tic
> methods are better than instance methods or vice versa.
>
> Most of the time instance methods are better. =A0That's because most of t=
he time
> you want actions to be controlled through an object, and not globally by =
a
> class. =A0The object that owns an instance method can carry state and con=
trol
> its behavior independently of other instances. =A0Concurrency is usually =
easier
> to handle with instance methods also.
>
> Notice the words "most of the time", "can carry", "usually easier". =A0Wh=
en in
> doubt, you probably want an instance method, but when behavior must inher=
e 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. =A0Factory methods, those =
that
> actually create class instances, will usually be static. =A0Class-wide
> behaviors, like registering instances with a class-level registry, have t=
o be
> static. =A0When 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 met=
hod.
> =A0 The PeopleInfo instance controls it nicely and holds related state in=
 a way
> that static variables probably would do less well. =A0Without 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.
0
Reply teser3 (98) 9/28/2008 9:58:07 PM

teser3@hotmail.com wrote:
>> tes...@hotmail.com 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
0
Reply arne6 (9481) 10/5/2008 1:13:44 AM

6 Replies
19 Views

(page loaded in 0.093 seconds)


Reply: