2014-09-11 100 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 

}