0

I'm trying to retrieve some data from my Db (I'm using SQLite) using DAO

public class ClassSectionDAO implements IClassSectionDAO{
  @Override
  public void selectAllClassSection() {
    d = new DBManager();
    sqliteQuery = new SqliteQueries();
    d.executeQuery(sqliteQuery.selectAllFrom("classSection"));
    try{
        while(d.getResultSet().next()){
            ClassSection classSection = new ClassSection();
            classSection.setClassSectionId(d.getResultSet().getString("classSectionId"));
            classSection.setSchoolClassCode(d.getResultSet().getString("schoolClassCode"));
            classSection.setClassSectionNumber(d.getResultSet().getInt("classSectionNumber"));
            classSection.setClassSectionAvailability(d.getResultSet().getString("classSectionAvailability"));
            classSectionList.add(classSection);
            System.out.println("classSectionList: " + classSectionList);
        }
    }
    catch(Exception e){
        Logger.getLogger(ClassSectionDAO.class.getName()).log(Level.SEVERE, null, e);
        classSectionList = null;
    }
    finally{
        d.closeDatabaseConnection();
    }
  }
}

All I'm getting is a list of objects like

classSectionList: [entities.classSection.ClassSection@19ccf6d, entities.classSection.ClassSection@1faf0e7, entities.classSection.ClassSection@1cf8409]

What should I do in order to get the values instead?

PS: If you want to see more code, let me know

2 Answers 2

1

I think you just made a typo, as you already said in your question: I get a list of Objects (of type ClassSection). That's clear because you print the list of Objects.

If you want to do it in the while loop (print immediately the value for every result), just change the last line in the loop to:

System.out.println("classSectionList: " + classSection.getSchoolClassCode());

If you want to print the values of all results (after all results are processed), just add Michaël's solution after the wile loop.

Sign up to request clarification or add additional context in comments.

Comments

1
for(ClassSection c : classSectionList) {
    System.out.println(c.getSchoolClassCode());
    // print other attributes
}

EDIT :

Firt solution :

public class ClassSectionDAO implements IClassSectionDAO{
  @Override
  public void selectAllClassSection() {
    d = new DBManager();
    sqliteQuery = new SqliteQueries();
    d.executeQuery(sqliteQuery.selectAllFrom("classSection"));
    try{
        while(d.getResultSet().next()){
            ClassSection classSection = new ClassSection();
            classSection.setClassSectionId(d.getResultSet().getString("classSectionId"));
            classSection.setSchoolClassCode(d.getResultSet().getString("schoolClassCode"));
            classSection.setClassSectionNumber(d.getResultSet().getInt("classSectionNumber"));
            classSection.setClassSectionAvailability(d.getResultSet().getString("classSectionAvailability"));
            classSectionList.add(classSection);

            // Solution of ProgrammingIsAwsome
            System.out.println("classSectionList: " + classSection.getSchoolClassCode());
        }
    }
    catch(Exception e){
        Logger.getLogger(ClassSectionDAO.class.getName()).log(Level.SEVERE, null, e);
        classSectionList = null;
    }
    finally{
        d.closeDatabaseConnection();
    }
  }
}

Second solution :

public class ClassSectionDAO implements IClassSectionDAO{
  @Override
  public void selectAllClassSection() {
    d = new DBManager();
    sqliteQuery = new SqliteQueries();
    d.executeQuery(sqliteQuery.selectAllFrom("classSection"));
    try{
        while(d.getResultSet().next()){
            ClassSection classSection = new ClassSection();
            classSection.setClassSectionId(d.getResultSet().getString("classSectionId"));
            classSection.setSchoolClassCode(d.getResultSet().getString("schoolClassCode"));
            classSection.setClassSectionNumber(d.getResultSet().getInt("classSectionNumber"));
            classSection.setClassSectionAvailability(d.getResultSet().getString("classSectionAvailability"));
            classSectionList.add(classSection);
        }
        // Print the list after the while
        for(ClassSection c : classSectionList) {
            System.out.println(c.getSchoolClassCode());
            // print other attributes
        }
    }
    catch(Exception e){
        Logger.getLogger(ClassSectionDAO.class.getName()).log(Level.SEVERE, null, e);
        classSectionList = null;
    }
    finally{
        d.closeDatabaseConnection();
    }
  }
}

1 Comment

You're right. But for all 3 records I am getting what I have in first record three times!That is [A1,A,1,null,A1,A,1,null,A1,A,1,null] How should I get all other values?

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.