2016-07-15 36 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++; 
} 

希望這有助於