Converting data types - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Converting data types

RibbletonRibbleton Posts: 152Member
Can't convert a ResultSet into a integer. Think i'm doing the conversion wrong. Thanks for your time!

[code]
int answer;

ResultSet correctAns = s1.getResultSet();

if (correctAns != null)
while( correctAns.next() )
{
System.out.println(correctAns.getString(1) );
}

answer = Integer.parseInt( correctAns ); // DOSE NOT LIKE

System.out.println("SDFSDSSV" + answer); // Test 'answer'
[/code]

My code complies and runs, but throws an exception. it's says:

ERROR:java.sql.SQLExeption; [Microsoft][ODBC Driver Manager] Invalid cursor state.

thanks
*Ribbleton_

Comments

  • Code_GuruCode_Guru Posts: 17Member
    : Can't convert a ResultSet into a integer. Think i'm doing the conversion wrong. Thanks for your time!
    :
    : [code]
    : int answer;
    :
    : ResultSet correctAns = s1.getResultSet();
    :
    : if (correctAns != null)
    : while( correctAns.next() )
    : {
    : System.out.println(correctAns.getString(1) );
    : }
    :
    : answer = Integer.parseInt( correctAns ); // DOSE NOT LIKE
    :
    : System.out.println("SDFSDSSV" + answer); // Test 'answer'
    : [/code]
    :
    : My code complies and runs, but throws an exception. it's says:
    :
    : ERROR:java.sql.SQLExeption; [Microsoft][ODBC Driver Manager] Invalid cursor state.
    :
    : thanks
    : *Ribbleton_
    :
    :
    You should look at the documentation for the Java API (http://java.sun.com/reference/api/index.html). I'm very surprised that your code compiles because, according to the docs, Integer.parseInt() will only take a String as a parameter, but you are passing correctAns which is a result set. Perhaps, you have made a copy and paste error and the line

    answer = Integer.parseInt( correctAns );

    should be

    answer = Integer.parseInt( correctAns.getString(1) );

    With that assumption, the problem is that this line of code is after the while loop that scrolls through the result set. I don't know much about JDBC, but I'm pretty sure that once ResultSet.next() returns false, the ResultSet is no longer useable (unless you can "rewind" it somehow). In other words, once your while loop is finished, the ResultSet is positioned past the end of the data, so you cannot retrieve anything.

    All this means is that you should put the Integer.parseInt() call inside the while loop.

    HTH
  • Code_GuruCode_Guru Posts: 17Member
    Also, if you check out the docs for ResultSet (http://java.sun.com/j2se/1.5.0/docs/api/java/sql/ResultSet.html), you will see that you are making this more complicated than necessary. There is a ResultSet.getInt() method that returns an int for you automagically. You should really make an effort to familiarize yourself with the API docs (see the link in my previous post). It will help you find the methods that are already available for you so you don't waste time re-inventing the wheel, so to speak.
  • RibbletonRibbleton Posts: 152Member
    : Also, if you check out the docs for ResultSet (http://java.sun.com/j2se/1.5.0/docs/api/java/sql/ResultSet.html), you will see that you are making this more complicated than necessary. There is a ResultSet.getInt() method that returns an int for you automagically. You should really make an effort to familiarize yourself with the API docs (see the link in my previous post). It will help you find the methods that are already available for you so you don't waste time re-inventing the wheel, so to speak.
    :


    Thanks, help a lot.
    Top guru
Sign In or Register to comment.