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 8028631
In Process

The Archive Base Latest Questions

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

This is a problem that I always encounter when I have to connect to

  • 0

This is a problem that I always encounter when I have to connect to a data base; how to separate SQL from the normal java code? I usually use a separate class for the Database connections, however, when you have several databases and several tables in each database, it is always difficult to do this 100%

As an example, if we want to put all the java SQL in a class named DBConnector.java, how do we code generically for different Insertions, Deletions, Data Retrieval, etc? What I’m thinking as the ideal case is that all the SQL statements should be in the same class and should be compatible with different flavors of same operation within the scope of the database application, thus providing a logical separation from rest of the code.

public void insertData (String db, String table, <Whatever Fields to be Inserted>)
{
  //generic SQL INSERT statement and execution 
}

public ResultSet retrieveData (String db, String table, <Whatever Fields Relevant>) 
{
  //generic retrieval of data
}

Is there any way to accomplish this? Or should we just add functionality for different flavors of Inserting, Querying, etc?

Thank you!

  • 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:11:27+00:00Added an answer on June 5, 2026 at 12:11 am

    If you want a sound architecture you’ll want at least few layers to seperate concerns.

    First of all, start off with model classes (most of the time, you’ll want one for every table in your database). Write them yourself, or have them generated automatically by using an ORM (e.g. EclipseLink, Hibernate). These should be POJO (Plain Old Java Objects), which means they are simple objects with properties (e.g. Name of type String, Id of type integer etc…). Your model objects should be carriers of data and nothing more (certainly no logic or processing).

    Then, make DAO (Data Access Objects) for all of the model classes (you can build a GenericDao class to inherit from if you want to). Here you will provide CRUD operations (insert, update, delete) via methods which take the model objects as arguments. This is database-backend specific, although you can insert a database-agnostic DAO-layer if you want.

    Third, have a service or manager layer per logical group of classes (this is the layer all frontend and controller code should be talking to for all wanted functionality). A typical method could be called registerCustomer(...) (which may use different DAO classes). Or findCustomerByName() etc.

    Structuring your application in this way is called MVC (Model – View – Controller), so that’s the term to google for if you want more information.

    This way, you typically will not have SQL queries any higher then the DAO layer, which means your application is a) maintainable and b) it’s easier to change backends later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I have this problem that I've never had before, it's really bugging me.
I have this problem that my sites uses alot of ajax and when a
So we have this problem that we are trying to figure out. Heres what
(I have a problem that I illustrated in this question but had no correct
I've got this problem that It always hit the PropertyAccessException when trying to saveOrUpdate();
There was this problem that has been asked about implementing a load byte into
The background to this problem is that I'm doing a computing project which involves
I think that this problem can be sorted using reflection (a technology which I'm
THIRD EDIT: I now believe that this problem is due to a SOAP version
Update: After some more reading I see that this problem is totally general, you

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.