When I am creating JDBC application, what I normally do is, hard coding the “sql statements” to the java program. For an example
ResultSet rs = st.execute("select * from Users")
But, I have heard that this approach is not good software engineering concept. Some say all these sql statements should stay on database as “stored procedures” and JDBC should access them. From these two approaches, which can be categorized as a good software engineering concept? Please help!
You won’t find concensus on this, I predict.
Stored procs are useful for encapsulating complex database logic and queries whilst avoiding transferring the data out of the database in order to sort/filter/query etc.
The downside is that you often find business logic creeping into stored procedures whereas it should likely remain in the application itself.
So there’s often a tug-of-war between developers/dbas etc as to where this logic should reside and how to use stored procs. I would suggest being pragmatic. Localise your SQL queries so that when you change the SQL (table-names etc.) you don’t have to change stuff across your entire codebase. Make use of stored procs for performance and when you’re doing stuff that is complex in code, but trivial for the database.