Convert Resultset to String array

前端 未结 5 951
死守一世寂寞
死守一世寂寞 2021-01-05 03:16

I need to Convert My result set to an array of Strings. I am reading Email addresses from the database and I need to be able to send them like:

message.addRe         


        
5条回答
  •  暖寄归人
    2021-01-05 03:46

    import java.util.List;
    import java.util.ArrayList;
    
    List listEmail = new ArrayList();
    
    while (rs.next()) {
        listEmail.add(rs.getString("EM_ID"));
    }
    //listEmail,toString() will look like this: [abc@abc.com, abc@def.com]
    //So lets replace the brackets and remove the whitspaces
    //You can do this in less steps if you want:
    String result = listEmail.toString();
           result = result.replace("[", "\"");
           result = result.replace("]", "\"");
           result = result.replace(" ", "");
    
    //your result: "abc@abc.com,abc@def.com"
    //If the spaces are e problem just use the string function to remove them
    

    Btw you may should use BCC instead of CC in terms of privacy....

    Also you should never use SELECT * FROM foo; Better use SELECT EM_ID FROM foo; This gives you a significant Performance increase in a huge Table, since the ResultSet just consists of the information you really need and use...

提交回复
热议问题