aspose file tools*
The moose likes JDBC and the fly likes Scaling PreparedStatement Update DB? 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 "Scaling PreparedStatement Update DB?" Watch "Scaling PreparedStatement Update DB?" New topic
Author

Scaling PreparedStatement Update DB?

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Previously when I was using a normal Statement object to update DB I created a class "UpdateDatabase" which took arguments of (tablename, setdata, wheredata) which I then used to build the SQL statement.

So my question is, when using PreparedStatements instead, is it is a good or a bad idea to do something similar?

Since I have two options

Option 1
Build a separate class for each specific update statement I want to run.

Option 2
Build one flexible class which takes the arguments as my previous one did.

So the question is, which is the best option technically, and why?

Thanks
Michael
Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
Michael Cropper wrote:Previously when I was using a normal Statement object to update DB I created a class "UpdateDatabase" which took arguments of (tablename, setdata, wheredata) which I then used to build the SQL statement.

So my question is, when using PreparedStatements instead, is it is a good or a bad idea to do something similar?

Since I have two options

Option 1
Build a separate class for each specific update statement I want to run.

Option 2
Build one flexible class which takes the arguments as my previous one did.

So the question is, which is the best option technically, and why?

Thanks
Michael


In my opinion, it's hard to tell which way is the best, but the option 2 is more flexible, using PreparedStatement.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61429
    
  67

Please take the time to choose an appropriate forum for your posts. This forum is for questions on Servlets, not JDBC. For more information, please click this link ⇒ CarefullyChooseOneForum.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
I think I have just answered my own question. It doesn't seem like it is possible to build a flexible script using the same approach that I was taking before.

When I created a new class and used the following within a prepared statement it didn't quite work.

Class...


PreparedStatement bit....


What happened is that when I was replacing the ?'s with the actual data from the arguments here is what was attempted to be put to the DB



So it wrapped the ' ' around the whole section and not just the actual data that is to be updated.

Was worth a go, but if this is how PreparedStatements behave, then I guess I will have to create a more specific class each time I need to interact with the DB (which is potentially a better option longer term mind)

Thanks
Michael
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Yes, writing generic database-updating code is generally a waste of time. It's one of those things which looks like a good idea at first but when you find out all of the details which said "generic" code has to deal with, it turns out to be not such a good idea.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

... however, I believe that creating a separate class for each SQL query is a reaction which goes too far in the opposite direction. I tend to write a class which groups together all of the SQL queries related to a particular type of object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scaling PreparedStatement Update DB?