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

が、これは

を役に立てば幸い
関連する問題