【问题标题】:Java retrieve data from sqlite return Objects instead of valuesJava从sqlite中检索数据返回对象而不是值
【发布时间】:2015-02-18 14:23:03
【问题描述】:

我正在尝试使用 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:如果您想查看更多代码,请告诉我

【问题讨论】:

    标签: java sqlite data-retrieval


    【解决方案1】:
    for(ClassSection c : classSectionList) {
        System.out.println(c.getSchoolClassCode());
        // print other attributes
    }
    

    编辑:

    第一解决方案:

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

    【讨论】:

    • 你是对的。但是对于所有 3 条记录,我得到了我在第一条记录中所拥有的 3 次!那就是 [A1,A,1,null,A1,A,1,null,A1,A,1,null] 我应该如何获得所有其他值?
    【解决方案2】:

    我认为您只是打错了,正如您在问题中已经说过的那样:我得到了一个对象列表(ClassSection 类型)。这很清楚,因为您打印了对象列表。

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-09-11
      • 2021-05-07
      • 2021-11-09
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 2020-06-29
      • 2016-02-27
      • 2018-12-18
      相关资源
      最近更新 更多