Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Resultset to String array

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.

like image 661
Stanley Mungai Avatar asked Apr 19 '13 14:04

Stanley Mungai


People also ask

How to convert ResultSet into array?

You should be able to easily iterate through the results on the result set, populating each array with the results as you interate. Something like this: ... ResultSet rs = st. executeQuery(query); ArrayList<String> names = new ArrayList<String>(); ArrayList<String> subjects = new ArrayList<String>(); while (rs.

Is ResultSet an array?

An Array object materializes the SQL ARRAY it represents as either a result set or a Java array. In the following statement, the ResultSet method getArray returns the value stored in the column ZIPS of the current row as the java.

Is ResultSet a string?

A ResultSet isn't a string, or anything resembling one. Logically speaking it is an array of maps. You need to traverse it, getting column values for each row, and do whatever you need to do with those.

What is Rs getString 1?

Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.


1 Answers

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);
}
like image 170
codeMan Avatar answered Sep 30 '22 14:09

codeMan