2014-12-19 75 views
0

我想從我的數據庫獲取一些數據(我使用SQLite)使用DAOJava的檢索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: [[email protected], [email protected], [email protected]] 

我應該怎麼做才能得到值呢?

PS:如果你想看到更多的代碼,讓我知道

回答

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

你是對的。但是,對於所有3條記錄,我在第一條記錄中獲得了三次![A1,A,1,null,A1,A,1,null,A1,A,1,null]'我應該如何獲得所有其他的價值? –

1

我想你剛剛犯了一個錯字,就像你在問題中已經說過的那樣:我得到一個對象列表(類型爲ClassSection)。這很明顯,因爲你打印了對象列表。

如果你想這樣做的,而環(打印立即每個結果的值),只是改變最後一行的循環中:

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

如果你想打印所有的值結果(處理完所有結果後),只需在wile循環後添加Michaël的解決方案即可。

相關問題