Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Converting data types Posted by Ribbleton on 26 Apr 2005 at 7:17 AM
Can't convert a ResultSet into a integer. Think i'm doing the conversion wrong. Thanks for your time!

     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'


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_

Report
Re: Converting data types Posted by Code_Guru on 26 Apr 2005 at 8:24 AM
: Can't convert a ResultSet into a integer. Think i'm doing the conversion wrong. Thanks for your time!
:
:
:      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'
: 

:
: 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
Report
Re: Converting data types Posted by Code_Guru on 26 Apr 2005 at 8:27 AM
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.
Report
Re: Converting data types Posted by Ribbleton on 27 Apr 2005 at 2:31 AM
: 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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.