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

  • Home
  • SEARCH
  • 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 5980707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:50:22+00:00 2026-05-22T21:50:22+00:00

I’m trying to determine which is the right way to code a DAO for

  • 0

I’m trying to determine which is the right way to code a DAO for a class that contains a few primitive data types, and three strongly typed objects which have DAOs of their own.

Here’s the class I’m implementing the DAO for, minus the getters/setters:

public class Course
{
    private int id;
    private int number;
    private String title;
    private Subject subject;
    private School school;
    private Instructor instructor;
    ...
}

The Subject, School, and Instructor classes all have DAOs of their own. So my initial thought was to use those DAOs to instantiate the Subject, School, and Instructor objects in the Course object (relevant code is in the try block):

public class CourseDAO
{
    public static Course selectCourse(int id)
    {
        ConnectionPool cp = ConnectionPool.getInstance();
        Connection c = cp.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        Course course = null;

        String query = "select id, number, title, subjectId, schoolId, instructorId " +
                "from course " +
                "where id = ? " +
                "limit 1";

        try
        {
            ps = c.prepareStatement(query);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next())
            {
                course = new Course();
                course.setId(rs.getInt(1));
                course.setNumber(rs.getInt(2));
                course.setTitle(rs.getString(3));
                course.setSubject(SubjectDAO.selectSubject(rs.getInt(4)));
                course.setSchool(SchoolDAO.selectSchool(rs.getInt(5)));
                course.setInstructor(InstructorDAO.selectInstructor(rs.getInt(6)));
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            cp.releaseConnection(c);
        }

        return course;
    }
}

The other DAOs look very similar to this one. Each gets a Connection from the ConnectionPool, uses a PreparedStatement to execute a simple query, closes the ResultSet and PreparedStatement, and releases the Connection back into the pool.

After implementing CourseDAO this way, I began to wonder if it would be better/more efficient to use a join in the query instead of calling SubjectDAO, SchoolDAO, and InstructorDAO, since each one use a Connection from the ConnectionPool. The query and try block would change as follows:

String query = "select course.id, course.number, course.title, " +
        "subject.id, subject.name, " +
        "school.id, school.name, school.abbreviation, school.type, school.city, school.region, school.country, " +
        "instructor.id, instructor.firstName, instructor.lastName " +
        "from course " +
        "inner join subject " +
        "on course.subjectId = subject.id " +
        "inner join school " +
        "on course.schoolId = school.id " +
        "inner join instructor " +
        "on course.instructorId = instructor.id " +
        "where course.id = ? " +
        "limit 1";

try
{
    ps = c.prepareStatement(query);
    ps.setInt(1, id);
    rs = ps.executeQuery();
    if (rs.next())
    {
        course = new Course();
        course.setId(rs.getInt(1));
        course.setNumber(rs.getInt(2));
        course.setTitle(rs.getString(3));

        Subject subject = new Subject();
        subject.setId(rs.getInt(4));
        subject.setName(rs.getString(5));

        School school = new School();
        school.setId(rs.getInt(6));
        school.setName(rs.getString(7));
        school.setAbbreviation(rs.getString(8));
        school.setType(rs.getString(9));
        school.setCity(rs.getString(10));
        school.setRegion(rs.getString(11));
        school.setCountry(rs.getString(12));

        Instructor instructor = new Instructor();
        instructor.setId(rs.getInt(13));
        instructor.setFirstName(rs.getString(14));
        instructor.setLastName(rs.getString(15));

        course.setSubject(subject);
        course.setSchool(school);
        course.setInstructor(instructor);
    }
}

In this case, which is the better choice? Use one database connection, join three tables, and return 15 fields? Or use four connections from the ConnectionPool (one for each DAO used), write less code, and execute simple queries?

  • 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-05-22T21:50:23+00:00Added an answer on May 22, 2026 at 9:50 pm

    You should definitelly use the method with joins. Doing query for each property is highly inefficient, and if you have a lot of data it will cause miserable performance due to n+1 select type of problem (3n+1 in your case).

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

Sidebar

Related Questions

No related questions found

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.