• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Scaling PreparedStatement Update DB?

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Maybe he went home and went to bed. And took this tiny ad with him:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic