gpt4 book ai didi

java - 将结果集转换为字符串数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:04:02 24 4
gpt4 key购买 nike

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

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

关于java - 将结果集转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16106914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com