Vassilis De
Vassilis De

Reputation: 373

Java retrieve data from sqlite return Objects instead of values

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

Upvotes: 0

Views: 63

Answers (2)

Michaël
Michaël

Reputation: 3671

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();
    }
  }
}

Upvotes: 1

ProgrammingIsAwsome
ProgrammingIsAwsome

Reputation: 1129

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.

Upvotes: 1

Related Questions