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

  while(rs.next())
  {
    out.print(rs.getString(1)); //divide the result into multiple values
  }
 %>

The result in above code may vary according to data fetched. Example of result is as below:

Google Facebook
or
Google
or 
Google Facebook Apple
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
Tiny Jaguar
  • 433
  • 3
  • 12
  • 30
  • 1
    Do you mean like [String.split()](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29)? – Jason Sperske Jan 24 '13 at 22:31
  • When you write `rs.getString(1)` you actually get the content of first column in the current row of the `ResultSet`. Therefore, according to which column you want the string (which string?) split? – GeorgeVremescu Jan 24 '13 at 22:34
  • When I write rs.getString(1), I am returned multiple results due to rs.next(). So if you suggest any way to get each of the values of the pf_nm column and store them for future use in the same page? – Tiny Jaguar Jan 24 '13 at 22:51

1 Answers1

1

If I understood your question and comment correctly then you can do something like this

ArrayList<String> cols = new ArrayList<String>();
while(rs.next())
  {
    cols.add(rs.getString(1));
    // Do something..
  }

Edit : What I understood from your previous question

String result = rs.getString(1); // gives "Google Facebook Apple AT&T" as result
String[] names = result.split("\\s"); // Split the line by whitespace

If you want you can also make use of ArrayList. You can also use hashMap if you required key values assoiciation (I am not sure thats what you want). Following are some useful links

1. Splitting string in java

2. How to use ArrayList in Java

3. HashMap example in Java

Here is complete pseudo code for you.

public class StringSplit {

    public static void main(String [] sm){
        String str = "Google Facebook Apple AT&T";
        // If you have more than one whitespace then better use \\s+
        String[] names = str.split("\\s");
        for(int i =0; i<names.length; i++){
            System.out.println(i +" : " + names[i]);
        }
    }
}

I hope this helps you.

Smit
  • 4,685
  • 1
  • 24
  • 28