2014-09-11 29 views
-2

我想從數據庫中使用servlet獲取值,它在while循環中檢索罰款只有我可以訪問從數據庫中檢索到的值,但我需要鞏固所有值成一個單一的對象。這裏是我的代碼,需要一個統一的對象值out while循環

String moduleId = request.getParameter("moduleId").trim(); 
String temp[] = moduleId.split("/"); 
for(String s:temp){ 
    String modId[]=s.split("/"); 
    PreparedStatement ps = null; 
    Connection connection=DatabaseConnection.getConnection(); 
    ps=connection.prepareStatement("select * from testcase_sahi where module_id=?"); 
    ps.setString(1,modId[0]); 
    ResultSet rs=ps.executeQuery(); 
    while(rs.next()){ 
     System.out.println(".............TC ID...."+rs.getString("testcase_id")); 
     System.out.println(".............TC Name...."+rs.getString("testcase_name")); 
     testCase=rs.getString("testcase_name"); 
     /*fos = new FileOutputStream(filename); 
      out = new ObjectOutputStream(fos); 
      System.out.println("****"+out.TC_OBJECT); 
      out.writeObject(testCase); 

      out.close();*/ 
    } 
+0

目前還不清楚你問什麼。結果應該是什麼? – Mureinik 2014-09-11 05:58:20

+0

是你的'單個對象'也許是一個列表(java.util.List)? – 2014-09-11 05:59:57

+0

謝謝,select語句的結果是兩個或兩個以上的值,while循環中要序列化整個結果,但這裏發生的是,序列化對象時,它只序列化最後的結果。 – Suganth 2014-09-11 06:05:58

回答

0

您只需將數據存儲在一個列表...

String moduleId = request.getParameter("moduleId").trim(); 
String temp[] = moduleId.split("/"); 
for(String s:temp){ 
    String modId[]=s.split("/"); 
    PreparedStatement ps = null; 
    Connection connection=DatabaseConnection.getConnection(); 
    ps=connection.prepareStatement("select * from testcase_sahi where module_id=?"); 
    ps.setString(1,modId[0]); 
    ResultSet rs=ps.executeQuery(); 


    List temporaryList = new ArrayList(); //HERE 

    while(rs.next()){ 
     System.out.println(".............TC ID...."+rs.getString("testcase_id")); 
     System.out.println(".............TC Name...."+rs.getString("testcase_name")); 
     testCase=rs.getString("testcase_name"); 

     temporaryList.add(testCase); //HERE 

    } 

    serializeData(temporaryList) //and HERE 

} 
相關問題