I need to Convert My result set to an array of Strings. I am reading Email addresses from the database and I need to be able to send them like:
message.addRecipient(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com");
Here is My code for reading the Email addresses:
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);
}
}
}
MyOutput is:
myemailaddress@abc.com
myotheremail@abc.com
I need it like this:
myemailaddress@abc.com,myotheremail@abc.com
I am using Oracle 11g.
If i understand correctly You want to see the output in one line with comma as a separator. Then instead of
Try
and remove last comma somehow.
Btw you may should use BCC instead of CC in terms of privacy....
Also you should never use SELECT * FROM foo; Better use SELECT EM_ID FROM foo; This gives you a significant Performance increase in a huge Table, since the ResultSet just consists of the information you really need and use...
to get the desired output:
replace these lines
by
Instead use:
you do not need
arr = em.split("\n");
since you are looping through each row (assuming that 1 row = 1 email address ) you just need this :