8

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, "[email protected],[email protected],[email protected]");

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:

[email protected]
[email protected]

I need it like this:

[email protected],[email protected]

I am using Oracle 11g.

2
  • 1
    To me, it looks like the split is working correctly, but your printed output will ALWAYS be on one line per item in the array ( as you are using System.out.println(). Don't you want to add the results of the split in to a StringBuilder and then System.out.println() that? Commented Apr 19, 2013 at 14:29
  • Did' tNnotice that. Yu are right Commented Apr 19, 2013 at 15:23

5 Answers 5

11

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

by

String arr = null;
while (rs.next()) {
    String em = rs.getString("EM_ID");
    arr = em.replace("\n", ",");
    System.out.println(arr);
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 That seems to be the right approach: rather than fixing the consequences of a split, avoid splitting in the first place! Don't forget to tell the OP that arr is no longer an array of strings, but a single string that he wants.
@codeMan depending of the query, isn't it dangerous to use String, it mights exceed string max length ?
5
System.out.println(arr[i]);

Instead use:

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

Comments

1

If i understand correctly You want to see the output in one line with comma as a separator. Then instead of

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

Try

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

and remove last comma somehow.

Comments

1

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 :

        ArrayList<String> arr = new ArrayList<String>();
        while (rs.next()) {
           arr.add(rs.getString("EM_ID"));
           System.out.println(arr.get(arr.size()-1));
        }

1 Comment

your array is not initialized, and there is now way to retrieve the number of rows returned without iterating over all of them.
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: [[email protected], [email protected]]
//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: "[email protected],[email protected]"
//If the spaces are e problem just use the string function to remove them

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.