0

How can I convert a ResultSet to an Integer array? I really need an Integer array. No Lists or something like that.

ResultSet rs = sqlite.query("SELECT ores FROM testtable WHERE nicknames='"+"testname"+"';");

"ores" contains 8 integers separated by a space: 10 20 30 40 50 60 70 80

Edit: "ores" is stored as VARCHAR

2
  • 1
    ores is an integer column and you are expecting 8 rows, or ores is a string with 8 values in it separated by a space? Commented Dec 6, 2012 at 16:00
  • Forgot to mention that sorry. "ores" is stored as VARCHAR. Commented Dec 6, 2012 at 16:07

4 Answers 4

3

Bad code. I'd recommend a PreparedStatement:

PreparedStatement ps = connection.prepareStatement("SELECT ores FROM testtable WHERE nicknames= ?");
ps.setString(1, nickname);
ResultSet rs = ps.executeQuery();
List<Integer> values = new ArrayList<Integer>();
while (rs.next()) {
    String ores = rs.getString("ores");
    String [] tokens = ores.split("\\s+");
    for (int i = 0; i < tokens.length; ++i) {
        values.add(Integer.valueOf(tokens[i]));
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I do not think this will work. He only has one row. You need to split the string (with a space delimiter) from the first row, and transform each one into an integer and add to the list/array.
skynorth is right. That doesn't work because there's only one row. The row is stored as a String (VARCHAR) an the integers are separated by a space
Sounds like a badly normalized design to me. Not even first normal form. Anyway, fixed the code.
2

The ResultSet is the result of yourr query, if you want to transform it to an ArrayList or a simple Integer array, you can iterate over the ResulstSet and add all the non-null Object to your ArrayList or array. This is a simple solution...

6 Comments

@downvoter: Downvoting without saying why is useless! From that ArrayList you can make an array, finally.
@Ingo: see here for how to make an array out of an ArrayList: docs.oracle.com/javase/6/docs/api/java/util/…
I think this was downvoted since you only have one row. You need to split the string from that row, and transform each one into an integer and THEN add to a list/array.
That's still not cear. He just said it was stored in varchar.
Well... It's ok. I switched to 8 columns now instead of storing the 8 integers in one varchar.
|
0
Vector<Integer> ores = new Vector<Integer>();
rs.beforeFirst();
while(rs.next()){ores.add(rs.getInt(1));}
Integer[] vals = ores.toArray(new Integer[0]);

Comments

-2

Maybe something like:

rs.last(); 
rowcnt = myResultSet.getRow(); // get row no.
rs.beforeFirst();

int i = 0;  
Integer[] options = new Integer[rowcnt];  
while (rs.next()) {  
  options[i] = Integer.parseInt(rs.getString(i));  
  i++;  
}  

5 Comments

Initializing the arraysize with the amount of columns and then iterate over al rows will garantee IndexOutOfBoundsExceptions.
There is only one column in the projection. So getColumnCount() will always return 1.
Corrected - thanks for the comments. (i understand the downvotes - if it is ok now, maybe it should at least not have them)
Better look twice ... rs.last() followed by rs.next() ... :)
@Fildor Thanks! Yes, better twice than sorry.. like me now :)

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.