java – 将Resultset转换为String数组

前端之家收集整理的这篇文章主要介绍了java – 将Resultset转换为String数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要将我的结果集转换为字符串数组.我正在从数据库中读取电子邮件地址,我需要能够发送它们,如:
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.

解决方法

获得所需的输出

替换这些线

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);
}
原文链接:https://www.f2er.com/java/127534.html

猜你在找的Java相关文章