aspose file tools*
The moose likes JDBC and the fly likes Database insert object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database insert object" Watch "Database insert object" New topic
Author

Database insert object

Ed Carrington
Ranch Hand

Joined: Oct 11, 2007
Posts: 77
I create an object in my servlet when inserting into my Oracle 9i database where 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:



I was also 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.
[ September 25, 2008: Message edited by: Ed Carrington ]
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
The short answer is if it is working as it is required to work for you, then it is the right way.


I would speculate that having the handler that does the insert is not as important if it is static or in an object each invocation, so much as how this handler internally works with the getting of the database connection handle and how it internally cleans up the connection object (releases the prepared statement and the connection).

From the java VM point of view, creating a new class instance every invocation, vs having a static method, where by either gets a connection from (a jndi bound data source?), does a jdbc insert statement, closes it, over time would likely be about the same. Though there might be slightly better performance from not having to create a new instance of the handler class each and every invocation, but that likely won't be the case if there are only a few created every day.

now for ease of extensibility or code reuse, it might be advantageous if the accessor was not a static method, but a class instance with a regular instance method. this could allow for future subclassing and overriding or extending the save() method to add new hooks so that additional things can be done when ever something is saved.

When using other frameworks, like spring, to wire up a handler class as a service, where you define a java class to be the data accessor and then 'wire' it up as a service via the bean factory, it really bahaves more like a static method, since the class instance for the data accessor service is usually instantiated when the spring container boots up, and then remains alive, and useable by your application (assuming your build the accessor bean in a thread safe manner) until it is shut down. though here we have the ability to decouple the datasource from the data accessor class.

I can post more examples on this if that would be helpful.


Error: Keyboard not attached. Press F1 to continue.
Ed Carrington
Ranch Hand

Joined: Oct 11, 2007
Posts: 77
Thanks for all your info! I am using direct jdbc connection and not jndi bound data source. My current method creating a new instance does work great but was wondering if I ever use it (in future projects) for something that has 30 to 40 database inserts per hour I would have a problem with performance?
[ September 27, 2008: Message edited by: Ed Carrington ]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Ed,
30-40 hits per hour is quite low. If you do have a problem with performance, the first step would be using a connection pool. That way less opening/closing resources would be done. The statements stay in your code. It's just that close() means "return to pool" rather than "close."


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ed Carrington
Ranch Hand

Joined: Oct 11, 2007
Posts: 77
Originally posted by Jeanne Boyarsky:
Ed,
30-40 hits per hour is quite low. If you do have a problem with performance, the first step would be using a connection pool. That way less opening/closing resources would be done. The statements stay in your code. It's just that close() means "return to pool" rather than "close."


Thanks, definitely not having performance issues using new instance method after trying it with 50 hits per hour.
Main thing I wanted to know is my database insert method (with new instance) is an acceptable practice in Java.
[ September 27, 2008: Message edited by: Ed Carrington ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

Sure, there's nothing wrong with using instance methods instead of class (static) methods. In fact there are reasons why you shouldn't use class methods in many cases. But that has nothing to do with JDBC in particular. If you want to debate the use of instance versus class methods you should post that question in one of the general Java forums here.
Ed Carrington
Ranch Hand

Joined: Oct 11, 2007
Posts: 77
Thanks, you are all very helpful as always in Javaranch. I appreciate your info and time on this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database insert object