将Resultset转换为String数组

时间:2013-04-19 14:24:04

标签: java arrays oracle11g resultset

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

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。

5 个答案:

答案 0 :(得分:10)

获得所需的输出:

替换这些行

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);
}

答案 1 :(得分:5)

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

改为使用:

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

答案 2 :(得分: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));
        }

答案 3 :(得分:1)

如果我理解正确您希望在逗号作为分隔符的一行中看到输出。 然后而不是

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

尝试

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

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

答案 4 :(得分: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; 更好用     从EMoo中选择EM_ID; 这样可以在巨大的表中显着提高性能,因为ResultSet只包含您真正需要和使用的信息......