Java从sqlite中检索数据返回Objects而不是值

时间:2014-12-19 15:59:53

标签: java sqlite data-retrieval

我正在尝试使用DAO从我的Db(我正在使用SQLite)中检索一些数据

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

我得到的只是一个像

这样的对象列表
classSectionList: [entities.classSection.ClassSection@19ccf6d, entities.classSection.ClassSection@1faf0e7, entities.classSection.ClassSection@1cf8409]

我应该怎样做才能获得价值?

PS:如果您想查看更多代码,请告诉我

2 个答案:

答案 0 :(得分:1)

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

编辑:

Firt解决方案:

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

第二个解决方案:

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 :(得分:1)

我认为你刚刚写了一个拼写错误,就像你在问题中已经说过的那样:我得到一个对象列表(ClassSection类型)。这很清楚因为你打印了对象列表。

如果你想在while循环中执行它(立即打印每个结果的值),只需将循环中的最后一行更改为:

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

如果要打印所有结果的值(在处理完所有结果之后),只需在wile循环后添加Michaël的解决方案。