0
<%
                        st = con.createStatement();
                        rs = st.executeQuery("select pf_nm from portfolio");

                    while(rs.next())
                        {
                           String arr= rs.getString(1);
                           out.println(arr+"\n\n");
                        }
 %>

As shown in above code, I get multiple values in return of getString(1),but I want all of them individually. How to get them?

For example, getString(1) returns Google Facebook Apple Adobe. I want to print them individually by storing them in some manner so that each of them is accessible thereafter.

Tiny Jaguar
  • 433
  • 3
  • 12
  • 30

1 Answers1

1

The return value is something like Google Facebook in String arr –

you can use String.split("\\s") to split your string with white space as a delimiter.

                       String arr= rs.getString(1);
                        String[] strArr = arr.split("\\s");
                        for(String s: strArr){
                            out.println(s);
                        }
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • This is showing OutOfBoundsException – Tiny Jaguar Jan 24 '13 at 21:46
  • @HarshMadhani i edited the code now, it'd throw OutOfBoundsException if the returned array length is less than 2, can you update your post with the code you have tried and also the stacktrace would be great .. :) – PermGenError Jan 24 '13 at 21:48
  • Sir,rather than this question answer can you please guide me following thing? When I get multiple values as result of getString(1) execution, as Google Facebook Apple AT&T ........., how to store each of them for further use? – Tiny Jaguar Jan 24 '13 at 21:52
  • As i said, you would have to split based on whitespace using string.split() which returns an string array, then you iterate over that array an get the individual values as in my answer, If you want these values to use them later in your app, store the array itself session scope and use them later by retriving the array from session. – PermGenError Jan 24 '13 at 21:57
  • Can you give the exact code for it? – Tiny Jaguar Jan 24 '13 at 22:14
  • @HarshMadhani please post it as a new question, i am sure there are many experts who would guide you thru, as per your current question you can refer to my answer.. good luck :) – PermGenError Jan 24 '13 at 22:16