Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:59:51+00:00 2026-06-05T00:59:51+00:00

I have the following doubt. When you have to query a DB from a

  • 0

I have the following doubt. When you have to query a DB from a Java application there are several ways to do it. I have came up with some approaches, but each of them have a con.

The first one is, you have a class eg. QueryManager which provides you with querying functionalities, like executeUpdate(...) and executeQuery(...) while hiding details of the management of connections etc. (Kind of a Facade Pattern). When you need to interact with the DB, you will pass the query as a String and manage the ResultSet.

The problem that I see is that if the DB changes, whether the DBMS or the DB itself, you will end up going file by file modifying the SQL. I see this as a huge dependency. Also, you are exposing to everyone the stucture of your DB and you are making each class to handle the ResultSet. On the other hand, you could achive higher modularity by using this method, since the models (I am a fan of MVC Pattern) of your classes could have package visibility.

The second idea that came to my mind was creating a QueryManager class that instead of providing you with the methods for querying, it will provide you with the methods you need. In other words, each time you need to use the DB, you will create a method in this class, with the SQL inside, that will return the information you need. But, the problem we are facing here is, you have to choose between returning a ResultSet or a model of the data you need.
The former, will make your classes dependant on the DB, less than in the previous example, since now there is no widely spread dependency with the DBMS, because all the SQL is contained in one class/file. However it still exists a dependency with the DB structure and you are exposing, as well, your DB structure to everyone.
The later implies that theese models no longer could be package visibility, they must be public, allowing any class to modify them, and breaking encapsulation.

Is there any other approach that solves all the previous problems? If not, which do you think is a better approach?

I do not think there is an absolute answear (maybe there is), but I must say that we are expecting changes in both the structure of the DB and in the DBMS. This might help in your answear. But try to make it as general as possible, since I could be in other project with the same doubt, but no with the same restrictions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:59:53+00:00Added an answer on June 5, 2026 at 12:59 am

    The second one is the good one: you should extract your data access methods in Data Access Objects (DAOs), which isolate the rest of the application from persistence-related problems. And DAOs should definitely return objects, and not result sets.

    This provides the following advantages:

    • decoupling of concerns and responsibilities
    • easier evolution of the rest of the application when the schema changes
    • easier evolution of the rest of the application when you choose to use an ORM instead of JDBC to access the database
    • easier unit-testing of the queries (and of the persistence layer in general), since the persistence code is not mixed with functional code
    • easier unit-testing of the business (service) layer, because it allows injecting mock DAOs to provide the data, rather than needing real data in the database to test it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with query from multiple tables. My database: I have following
I have some doubt about the title, but I couldn't come up with anything
I have the following Linq query: List<MonthlySales> monthlySales = (from sale in Sales group
I have been struggling with the following query: select * from table_name where contains((field1,field2),'S.E.N.S');
recently I am learning C++ and have some doubt on the following case. void
I have the following code snippet in QML, but I doubt statements inside Repeater
In following program . I have one doubt. I have declared one global variable
I have seen the following question but I still have a few doubts. Sending
I have following requirement, I have C#/.Net console application, which refers to 'System.Data.Sqlite.dll' 'System.Data.Sqlite.dll'
I have following code in my application: // to set tip - photo in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.