2

I have a List getter method that returns a ResultSet. Everytime the ResultSet loops this was added to List. But in my ActionListener I have a loop that changes the values everytime and also it gives another ResultSet. How can I avoid List to create another instance when calling the getter method?

public List <Model> get(Model model) 
{
    String query = "{call get(?,?,?,?)}";
    List <Model> list = new ArrayList();

    try(Connection con = DBUtil.getConnection(DBType.MYSQL);
        CallableStatement cs = con.prepareCall(query);)
    {
        cs.setInt(1, model.getName());
        cs.setString(2, model.getPosition());
        cs.setString(3, model.getDepartment());
        cs.setString(4, model.getDate());

        try(ResultSet rs = cs.executeQuery())
        {
            while(rs.next())
            {
                Model m = new Model();

                m.setIn(rs.getString(1));
                m.setOut(rs.getString(2));

                list.add(m);

            }
        }
    }
    catch(SQLException ex)
    {
        System.err.println("Error "+ex.getSQLState());
    }
    return list;
}

Calling this in action listener.

for(int i = 0; i < myList.size(); i++)
{
    model.setName("Anything");
    model.setPosition("Jr");
    model.setDepartment("It");
    model.setDate(myList.get(i));

    DAO.get(model);
}

As you can see here loop gives new value everytime it loops. My problem is getter method resets to zero after executing one row at resultset loop.

2
  • Keep a "total" list in your calling code, and add the results of get() to that list on each iteration? Commented May 24, 2017 at 8:37
  • @JonSkeet Yes exactly. By the way I already the issue is already solve. :) Commented May 24, 2017 at 8:55

1 Answer 1

1

If you want to gather the elements from all get() invocations, there are two solutions.

Either collect the returned elements into a single list in the calling loop:

ArrayList<Model> list = new ArrayList<>(); 
for(int i = 0; i < myList.size(); i++)
{
    model.setName("Anything");
    model.setPosition("Jr");
    model.setDepartment("It");
    model.setDate(myList.get(i));

    list.addAll(DAO.get(model)); //Get elements, and add them to the list
}

... Or change public List <Model> get(Model model) to public List<Model> get(Model model, List<Model> list) and instead of creating a new list for each invocation of get(), pass it the same list:

public List<Model> get(Model model, List<Model> list) 
{
    String query = "{call get(?,?,?,?)}";

    try(Connection con = DBUtil.getConnection(DBType.MYSQL);
        CallableStatement cs = con.prepareCall(query);)
    {
        cs.setInt(1, model.getName());
        cs.setString(2, model.getPosition());
        cs.setString(3, model.getDepartment());
        cs.setString(4, model.getDate());

        try(ResultSet rs = cs.executeQuery())
        {
            while(rs.next())
            {
                Model m = new Model();

                m.setIn(rs.getString(1));
                m.setOut(rs.getString(2));

                list.add(m);

            }
        }
    }
    catch(SQLException ex)
    {
        System.err.println("Error "+ex.getSQLState());
    }
    return list;
}

Then invoke get() like this:

ArrayList<Model> list = new ArrayList<>(); 
for(int i = 0; i < myList.size(); i++)
{
    model.setName("Anything");
    model.setPosition("Jr");
    model.setDepartment("It");
    model.setDate(myList.get(i));

    DAO.get(model,list);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your solution # 2 and absolutely works. It's also readable. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.