Converting results to an array of strings

I need to convert my result set to an array of strings. I am reading email addresses from a database and I need to be able to send them as:

message.addRecipient(Message.RecipientType.CC, "abc@abc.com,abc@def.com,ghi@abc.com");

Here is my code for reading 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:

myemailaddress@abc.com
myotheremail@abc.com

I need it like this:

myemailaddress@abc.com,myotheremail@abc.com

I am using Oracle 11g.

+5
source share
5 answers

to get the desired result:

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

String arr = null;
while (rs.next()) {
    String em = rs.getString("EM_ID");
    arr = em.replace("\n", ",");
    System.out.println(arr);
}
+8
source
System.out.println(arr[i]);

Use instead:

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

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

, .

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

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

- .

+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

Btw you can use BCC instead of CC in terms of privacy.

Also you should never use SELECT * FROM foo; Best use of SELECT EM_ID FROM foo; This gives you a significant increase in performance in a huge table, since the ResultSet consists only of the information that you really need and uses ...

+1
source

All Articles