2016-07-15 1 views
1
public static List<SPACE_CreateLicenseModel> SPACE_getDetails() throws ClassNotFoundException, FileNotFoundException, JSONException{ 

    SPACE_CreateLicenseModel view = new SPACE_CreateLicenseModel(); 
    Statement stmt = null; 
    Connection connect = null; 
    List<SPACE_CreateLicenseModel> allData = new ArrayList<SPACE_CreateLicenseModel>(); 
    try { 
     connect = SPACE_DBController.SPACE_getConnection(); 
     stmt = connect.createStatement(); 
     JSONObject obj = SPACE_Parse.parse ("C:/Users/Rachana/workspace/SPACEOM/WebContent/Data/SPACE_Database.json"); 
     String tablename = obj.getString("table_name"); 
     String sql = "SELECT * FROM " + tablename + " WHERE (SPLD_LicenseActiveStatus <> 5 OR SPLD_LicenseActiveStatus IS NULL)"; 
     ResultSet result = stmt.executeQuery(sql); 
     int i =0; 
      while (result.next()) {  
       view.setSPLD_DeviceID_Mfg(result.getString(1)); 
       view.setSPLD_DeviceID_ModelNo(result.getString(2)); 
       view.setSPLD_DeviceID_SrNo(result.getString(3)); 
       view.setSPLD_DeviceID_Search_mode(result.getByte(4)); 
       view.setSPLD_LicenseType(result.getByte(5)); 
       view.setSPLD_LicenseTypeChangedDate(result.getDate(6)); 
       view.setSPLD_LicenseActiveStatus(result.getByte(7)); 
       view.setSPLD_LicenseActiveDate(result.getDate(8)); 
       view.setSPLD_LicenseAccess(result.getByte(9)); 
       view.setSPLD_LicenseAccessMaxNo(result.getInt(10)); 
       view.setSPLD_LicenseAccessCounter(result.getInt(11)); 
       view.setSPLD_LicenseStartDate(result.getDate(12)); 
       view.setSPLD_LicenseExpiryDate(result.getDate(13)); 
       view.setSPLD_LicenseeOrg(result.getString(14)); 
       view.setSPLD_LicenseeAddress(result.getString(15)); 
       view.setSPLD_LocationActive(result.getString(16)); 
       view.setSPDL_Longitude(result.getDouble(17)); 
       view.setSPDL_Latitude(result.getDouble(18)); 
       view.setSPDL_LocationTolerance(result.getFloat(19)); 
       view.setSPLD_FutureOption1(result.getString(20)); 
       view.setSPLD_FutureOption2(result.getString(21)); 
       view.setSPLD_FutureOption3(result.getString(22)); 
       view.setSPLD_FutureOption4(result.getInt(23)); 
       view.setSPLD_FutureOption5(result.getInt(24)); 
       view.setSPLD_StatCounter1_FirstUseDate(result.getDate(25)); 
       view.setSPLD_StatCounter2_MessageTotal(result.getInt(26)); 
       view.setSPLD_StatCounter3_FailedAttempts(result.getInt(27)); 
       view.setSPLD_StatCounter4_FirstFailedAttemptDate(result.getDate(28)); 
       view.setSPLD_StatCounter5_LastFailedAttemptDate(result.getDate(29)); 
       view.setSPLD_StatCounter6(result.getInt(30)); 
       view.setSPLD_StatCounter7(result.getInt(31)); 
       view.setSPLD_StatCounterOption1(result.getString(32)); 
       view.setSPLD_StatCounterOption2(result.getString(33)); 
       view.setSPLD_StatCounterOption3(result.getString(34)); 
       view.setSPLD_StatCounterOption4(result.getInt(35)); 
       view.setSPLD_StatCounterOption5(result.getInt(36)); 
       view.setSPLD_MainContact1Name(result.getString(37)); 
       view.setSPLD_MainContact2Name(result.getString(38)); 
       view.setSPLD_MobileNo1(result.getString(39)); 
       view.setSPLD_MobileNo2(result.getString(40)); 
       view.setSPLD_EmailID1(result.getString(41)); 
       view.setSPLD_EmailID2(result.getString(42)); 
       view.setSPLD_CustomerDetailOption1(result.getString(43)); 
       view.setSPLD_CustomerDetailOption2(result.getString(44)); 
       view.setSPLD_BroadCastGEN1(result.getString(45)); 
       view.setSPLD_BroadCastGEN2(result.getString(46)); 
       view.setSPLD_BroadCastID1(result.getInt(47)); 
       view.setSPLD_DevSpecGEN1(result.getString(48)); 
       view.setSPLD_DevSpecGEN2(result.getString(49)); 
       view.setSPLD_DevSpecGEN3(result.getString(50)); 
       view.setSPLD_DevSpecID1(result.getInt(51)); 
       view.setSPLD_DevSpecID2(result.getInt(52)); 
       view.setSPLD_MessageStatus(result.getString(53).charAt(0)); 
       allData.add(i,view); 
       i++; 
      } 
    } catch (SQLException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (ClassNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    }finally{ 
      //finally block used to close resources 
      try{ 
      if(stmt!=null) 
       stmt.close(); 
      }catch(SQLException se2){ 
      }// nothing we can do 
      try{ 
      if(connect!=null) 
       connect.close(); 
      }catch(SQLException se){ 
      se.printStackTrace(); 
      } 

    } 
    return allData; 

} 

데이터베이스의 모든 행을 가져 와서 배열에 저장하고 있습니다. 그러나 마지막 행만 표시하는 동안 인쇄됩니다. 목록 요소가 무시되고 있습니다. 즉, allData.add (1, view), allData.add (2, view), allData.add (3, view), allData.add (4, view) 등은 모두 동일합니다. 당신은 루프의 각 반복에 대한 새로운 객체를 생성하지 않는 것처럼데이터베이스 행이 개체 목록으로 읽는 중

답변

1

, 동일한 개체를 다시 사용하므로

Statement stmt = null; 
Connection connect = null; 
List<SPACE_CreateLicenseModel> allData = new ArrayList<SPACE_CreateLicenseModel>(); 
try { 
    connect = SPACE_DBController.SPACE_getConnection(); 
    .... 
     while (result.next()) {  
      SPACE_CreateLicenseModel view = new SPACE_CreateLicenseModel(); 
0

이 while 루프의 모든 반복에 새로운 뷰 객체를 생성하려고합니다. 동일한 뷰 객체를 반복 할 때마다 객체가 메모리에 기록됩니다. 루프에 위의 라인을 추가 루프는 당신이 당신의 데이터를 인쇄 할 때 표시되는 마지막 행의 값으로 대체 실행 마지막 시간 ...

while(yourCondition){ 
    view = new SPACE_CreateLicenseModel(); 
    //your code goes here.... 
} 

새로운 뷰 객체를 생성하고있을 것 allData 변수에 추가되었습니다.

1

원인 : 각 행에 동일한 개체가 따라서 업데이트지고 목록에있는 모든 오브젝트가 같은 값 (마지막 행)가 현재에 대한

.

해상도 :

당신은 모든 행에 대해 루프 SPACE_CreateLicenseModel 각 시간을 초기화해야합니다.

while (result.next()) {  
     SPACE_CreateLicenseModel view = new SPACE_CreateLicenseModel(); 
     view.setSPLD_DeviceID_Mfg(result.getString(1)); 
. 
. 
     allData.add(i,view); 
     i++; 
} 

희망이

을하는 데 도움이
관련 문제