需要while循环中的合并对象值

时间:2014-09-11 05:54:56

标签: java servlets

我正在尝试使用servlet从数据库中获取值,并且它在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();*/
    }

1 个答案:

答案 0 :(得分: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

}