8

我需要将我的结果集转换为字符串数组。我正在从数据库中读取电子邮件地址,我需要能够像这样发送它们:

message.addRecipient(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com");

这是我阅读电子邮件地址的代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test {

    public static void main(String[] args) {
        Connection conn = null;
        String iphost = "localhost";
        String dbsid = "ASKDB";
        String username = "ASKUL";
        String password = "askul";

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String sql = "SELECT * FROM EMAIL";
            conn = DriverManager.getConnection("jdbc:oracle:thin:@" + iphost + ":1521:" + dbsid, username, password);
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            String[] arr = null;
            while (rs.next()) {
                String em = rs.getString("EM_ID");
               arr = em.split("\n");
               for (int i =0; i < arr.length; i++){
                   System.out.println(arr[i]);
               }
            }
        } catch (Exception asd) {
            System.out.println(asd);
        }
    }
}

我的输出是:

myemailaddress@abc.com
myotheremail@abc.com

我需要这样的:

myemailaddress@abc.com,myotheremail@abc.com

我正在使用 Oracle 11g。

4

5 回答 5

11

获得所需的输出:

替换这些行

String[] arr = null;
while (rs.next()) {
    String em = rs.getString("EM_ID");
    arr = em.split("\n");
    for (int i =0; i < arr.length; i++){
        System.out.println(arr[i]);
    }
}

经过

String arr = null;
while (rs.next()) {
    String em = rs.getString("EM_ID");
    arr = em.replace("\n", ",");
    System.out.println(arr);
}
于 2013-04-19T14:26:52.513 回答
5
System.out.println(arr[i]);

而是使用:

System.out.print(arr[i] + ",");
于 2013-04-19T14:26:30.450 回答
1
import java.util.List;
import java.util.ArrayList;

List<String> listEmail = new ArrayList<String>();

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

顺便说一句,就隐私而言,您可能应该使用 BCC 而不是 CC....

你也不应该使用 SELECT * FROM foo; 更好地使用 SELECT EM_ID FROM foo; 这使您在一个巨大的表中显着提高了性能,因为 ResultSet 只包含您真正需要和使用的信息......

于 2013-04-19T15:22:41.337 回答
1

如果我理解正确你想在一行中看到输出,用逗号作为分隔符。然后代替

System.out.println(arr[i]);

尝试

System.out.print(arr[i]+",");

并以某种方式删除最后一个逗号。

于 2013-04-19T14:37:31.770 回答
1

你不需要arr = em.split("\n");,因为你正在遍历每一行(假设 1 行 = 1 个电子邮件地址)你只需要这个:

        ArrayList<String> arr = new ArrayList<String>();
        while (rs.next()) {
           arr.add(rs.getString("EM_ID"));
           System.out.println(arr.get(arr.size()-1));
        }
于 2013-04-19T14:30:48.020 回答