Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scaling PreparedStatement Update DB?

 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic