Java – convert resultset to string array
•
Java
I need to convert my result set to a string array I'm reading email addresses from the database, and I need to be able to send them, such as:
message.addRecipient(Message.RecipientType.CC,"abc@abc.com,abc@def.com,ghi@abc.com");
This is the code I read the email address:
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);
}
}
}
My output is:
myemailaddress@abc.com myotheremail@abc.com
I need this:
myemailaddress@abc.com,myotheremail@abc.com
I am using Oracle 11g
Solution
Get the desired output:
Replace these lines
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]);
}
}
adopt
String arr = null;
while (rs.next()) {
String em = rs.getString("EM_ID");
arr = em.replace("\n",",");
System.out.println(arr);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
