0

what I want is to get all the values that are into a column that i call in query from Java to SQL Server, I think it is like this:

public ArrayList createArray(ResultSet data){
    try {
        ArrayList arrayData = (ArrayList) data.getArray(1);//get all the data from the resultSet that's into the column 1
        return arrayData;
    } catch (SQLException ex) {/*Error message*/return null;}
}

But actually I don't know what does getArray() returns, and I don't find any information about it. If someone can help, I'll be thankful. Also if getArray() doesn't work like I think it does, could you please tell me how to do what I want?

8
  • is this your code? if it is your code how you do not know what getArray returns here? Commented Jul 9, 2014 at 1:30
  • can you post your code which is related how your retrieve data? Commented Jul 9, 2014 at 1:40
  • i only want the data from the fist column called in the query, so that's what it should return, but what i need is to know what does getArray do?, i mean i created the code, but just assuming that getArray returns all the data called from a column Commented Jul 9, 2014 at 1:40
  • the only point that I can tell you is you may have a array which you want to access its content by using getArray method. it is called getters Commented Jul 9, 2014 at 1:41
  • ok, is there any way to get all the rows data from a single column of a query and set them into a List or Array using jdbc? Comments use mini-Markdown formatting: link italic bold code. The post author will always be notified of your comment. To also notify a previous commenter, mention their user name: @peter or @PeterSmith will both work. Learn more… Commented Jul 9, 2014 at 1:44

2 Answers 2

1

Here is the standard jdbc approach, which requires lots of boilerplate code and temporary variables, and the same thing with a more elegant add-on library, jdbi, which lets you use a much simpler fluent API:

jdbi approach:

public static List<String> jdbiEmployeeNameQuery(Handle h) {
    return h.createQuery("select name from employees order by id").map(StringMapper.FIRST).list();
}

jdbc approach:

public static List<String> jdbcEmployeeNameQuery(Connection dbConnection) throws SQLException {
    try (Statement s = dbConnection.createStatement()) {
        try (ResultSet rs = s.executeQuery("select name from employees order by id")) {
            List<String> names = new ArrayList<String>();

            while (rs.next()) {
                names.add(rs.getString(1));
            }

            return names;
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

is there any similar in jdbc ?
Yes, straight JDBC just involves a ton of boilerplate code.
:s, can you give me an example?
there you go! Added to the answer.
0

try something like this :

List list = new ArrayList();
while (resultSetObject.next()) {
    list.add(resultSetObject.getString("columnName"));
} 

You can convert it to String array if you want which I think is not necessary

String[] arrOfString = (String[]) list.toArray(new String[list.size()]);

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.