Remember that when a ResultSet is returned, the cursor points above the first record in the ResultSet. In order to access the first record, you need to call res.next() to move the pointer.
I'd recommend changing your code to:
public void extractValues(ResultSet res) throws SQLException
{
String values[] = new String[4];
// Continue only if RS has rows
if (res.next())
{
for (int i = 0; i<4; i++)
values = res.getString(i+1);
}
}
Of course, this doesn't allow you to do anything with the values so I'm not sure why you are doing this. It makes more sense to use the ResultSet where you create it unless you make use of ResultSetMetaData to get info about the result set before you get the info. Otherwise a helper method like this is not at all re-usable. For instance:
public String [] extractValues(ResultSet res) throws SQLException
{
ResultSetMetaData rsmd = res.getMetaData();
int columns = rsmd.getColumnCount();
String values[] = new String[columns];
// Continue only if RS has rows
if (res.next())
{
for (int i = 0; i<columns; i++)
values = res.getString(i+1);
}
return values;
}
Of course this only returns the first row but you could extend the idea to process an entire result set using a List or something else.
Charles is right. There is no point in extracting the values from a resultset when you can use it directly. However, no offense Charles, but there is a flaw in the program. You can't assign a String to an array; values=res.getString(i+1). So this is what I think should be correct:-
public String [] extractValues(ResultSet res) throws SQLException
{
ResultSetMetaData rsmd = res.getMetaData();
int columns = rsmd.getColumnCount();
String values[] = new String[columns];
// Continue only if RS has rows
if (res.next())
{
for (int i = 0; i<columns; i++)
values = res.getString(i+1);
}
return values;
}
Best Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best
You are right Leon. I re-edited this a couple of times cause I was working on a couple of different angles and I forgot the array indexing in the final version...
Unfortunately I don't see what you did to fix my problem.
Well Leon, it would appear that there is a problem with converting the array brackets in the message. I may not have left them out after all but neither of our posts show them and I know that I put them in the last post.
I didn't notice mine was missing too...haha.... anyway glad that ewan got his solution
Best Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.