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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:47:00+00:00 2026-06-13T09:47:00+00:00

There are thee tables inside my database. One is employee , the second is

  • 0

There are thee tables inside my database. One is employee, the second is employee_Project, and the third is employee_Reporting. Each table has a common employee_Number as its primary key, and there is a one to many relationship among them such that an employee has many projects and reporting dates.

I have run select * from employee, select * from employee_project, select * from employee_reporting in three data holder classes which have methods fillResultSet(Result set) and List<T> getData(). This is based on a SqlDbEngine class with a runQuery(PreparedStatement,DataHolder) method, and the implementation has been completed.

Now I have to design a getAllEmployee() method along with project and reporting detail with optimal code in java using JDBC. I have used an iterator but this solution is not acceptable; now I have to use a foreach loop.

This is what I have done:

public List<Employee> getAllEmployees() {
    EmployeeDataHolderImpl empdataholder = new EmployeeDataHolderImpl();
    List<Employee> list_Employee_Add = null;

    try {
        Connection connection = mySqlDbConnection.getConnection();
        PreparedStatement preparedStatement = connection
                .prepareStatement(GET_ALL_EMPLOYEE_DETAILS);
        mySqlDBEngineImpl.runQuery(preparedStatement, empdataholder);
    } catch (SQLException e) {

        e.printStackTrace();
    }
    for (Employee employee : empdataholder.getData()) {
        new EmployeeDAOImpl().getProject(employee);
                    new EmployeeDAOImpl.getReport(employee);
    }
    list_Employee_Add = empdataholder.getData();
    return list_Employee_Add;

}

and make another method

    public void getProject(Employee emp) {
    EmployeeProjectDataHolderImpl employeeProjectHolder = new EmployeeProjectDataHolderImpl();
    try {
        Connection connection = mySqlDbConnection.getConnection();
        PreparedStatement preparedStatement = connection
                .prepareStatement(GET_ALL_PROJECT_DETAILS);
        mySqlDBEngineImpl
                .runQuery(preparedStatement, employeeProjectHolder);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    for (EmployeeProject employee_Project : employeeProjectHolder.getData()) {
        if (employee_Project.getEmployeeNumber() == emp.getEmpNumber()) {
            emp.getProjects().add(employee_Project);
        }
    }
}

    public void getReport(Employee emp) {
    EmployeeReportDataHolderImpl employeeReportHolder = new EmployeeReportDataHolderImpl();
    try {
        Connection connection = mySqlDbConnection.getConnection();
        PreparedStatement preparedStatement = connection
                .prepareStatement(GET_ALL_REPORT_DETAILS);
        mySqlDBEngineImpl
                .runQuery(preparedStatement, employeeReportHolder);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    for (EmployeeReport employee_Report : employeeReportHolder.getData()) {
        if (employee_Report.getEmployeeNumber() == emp.getEmpNumber()) {
            emp.getProjects().add(employee_Project);
        }
    }
}
}

and same for Employee Reporting but doing, this performance is going to decrease.no body worry about closing connection i will do it

Please tell me how I could improve my solution..

  • 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-13T09:47:01+00:00Added an answer on June 13, 2026 at 9:47 am

    If you use your actual code, you will have 3 impacts in your code:

    • You’re opening a connection to get the employee’s data.
    • For every employee, you open (and close) a new connection to get his projects.
    • For every employee, you open (and close) a new connection to get his reports.

    Note that opening a new connection is a performance hit on your application. It doesn’t matter if you use an enhanced for-loop or an Iterator, there would be many hits that can slow down your application.

    Two ways to solve this problem:

    1. Open a single connection where you run all your select statements. This will be better than opening/closing lot of connections.

    2. Create a single SQL statement to retrieve the employees and the data you need for every employee. It will have better performance for different reasons:

      • A single connection to the database.
      • A single query instead of lot of queries to the database (a single I/O operation).
      • If your rdbms allows it, the query will be optimized for future requests (a single query instead of multiple queries).

    I would prefer to go with the second option. For this, I tend to use a method that executes any SQL select statement and return a ResultSet. I’ll post a basic example (note, the provided code can be improved depending on your needs), this method could be in your SqlDbEngine class:

    public ResultSet executeSQL(Connection con, String sql, List<Object> arguments) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = con.prepareStatement(sql);
            if (arguments != null) {
                int i = 1;
                for(Object o : arguments) {
                    pstmt.setObject(i++, o);
                }
            }
            //method to execute insert, update, delete statements...
            rs = pstmt.execute();
        } catch(SQLException e) {
            //handle the error...
        }
        return rs;
    }
    

    And this other method to handle all the query operation

    public List<Employee> getAllEmployee() {
        Connection con = null;
        ResultSet rs = null;
        List<Employee> lstEmployee = new ArrayList<Employee>();
        try {
            con = mySqlDbConnection.getConnection();
            //write the sql to retrieve all the data
            //I'm assuming these can be your columns, it's up to you
            //this can be written using JOINs...
            String sql = "SELECT E.EMPLOYEE_ID, E.EMPLOYEE_NAME, P.PROJECT_NAME, R.REPORT_NAME FROM EMPLOYEE E, PROJECT P, REPORT R WHERE E.EMPLOYEE_ID = P.EMPLOYEE_ID AND E.EMPLOYEE_ID = R.EMPLOYEE_ID";
            //I guess you don't need parameters for this...
            rs = SqlDbEngine.executeSQL(con, sql, null);
            if (rs != null) {
                Employee e;
                int employeeId = -1, lastEmployeeId = -1;
                while (rs.next()) {
                    //you need to make sure to create a new employee only when
                    //reading a new employee id
                    employeeId = rs.getInt("EMPLOYEE_ID");
                    if (lastEmployeeId != employeeId) {
                        e = new Employee();
                        lastEmployeeId = employeeId;
                        lstEmployee.add(e);
                    }
                    Project p = new Project();
                    Report r = new Report();
                    //fill values of p...
                    //fill values of r...
                    //you can fill the values taking advantage of the column name in the resultset
                    //at last, link the project and report to the employee
                    e.getProjects().add(p);
                    e.getReports().add(r);
                }
            }
        } catch (Exception e) {
            //handle the error...
        } finally {
            try {
                if (rs != null) {
                    Statement stmt = rs.getStatement();
                    rs.close();
                    stmt.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                //handle the error...
            }
        }
        return lstEmployee;
    }
    

    Note that the second way can be harder to code but it will give you the best performance. It’s up to you to improve the provided methods, some advices:

    • Create a class that receives a ResultSet and builds a Project instance using the columns name of the ResultSet (similar for Report and Employee).
    • Create a method that handles the ResultSet and its Statement close.
    • As a best practice, never use select * from mytable, it’s preferable to write the needed columns.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a column that exists in 2 tables. In table 1, this column
There are two table s : one is the master and one the detail
There is one thing I really love about LXML, and that the E builder.
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
I have thee tables user_follow, images, users My user_id : 3 My Friend user_id
I have several tables in my database that have read-only fields that get set
Oh great Stackoverflow, I beseech thee... I need to do the following: CREATE UNIQUE
Essentially, I have a binary voting system Like/Dislike. Thee class is called Like It
There is a moment in my app, that I need to force to show
There is a directed graph having a single designated node called root from which

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.