File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to store the count value returned froma  sql query into a variable for future use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to store the count value returned froma  sql query into a variable for future use? " Watch "How to store the count value returned froma  sql query into a variable for future use? " New topic
Author

How to store the count value returned froma sql query into a variable for future use?

Yuta Lolap
Ranch Hand

Joined: Jun 03, 2012
Posts: 80

I was facing an error in storing the row count obtained from sql into a variable i displayed the one file running code below along with output. i was wondering if you guys could help me with this error. I want to count the no of customers and then append it to a string and generate the customer id.



I want to get he count of customers from a table in oracle 11g db and store it in a variable. I am unable to do so. Please can anyone help me? I have shown the snippet of the code i am trying to write...

OUTPUT:
Connection Successfull
sun.jdbc.odbc.JdbcOdbcConnection@39b27b
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(Unknown Source)
at components.genrateid.generateid(genrateid.java:29)
at components.genrateid.main(genrateid.java:43)
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 150

First of all, your class name, genrateid, is too similar to your function name, generateid. It's confusing.

How do you know you're unable to get the row count? Have you printed the value of the variable "count" to verify it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Before you can access the columns from a row of the ResultSet, you have to call "rs.next()" to move to that row. Since your query is guaranteed to return exactly one row, just call rs.next() before you try to get data from the ResultSet and leave it at that. Normally you use rs.next() to control a loop (you've seen the JDBC tutorials, right?) but in this case a loop isn't necessary.
Yuta Lolap
Ranch Hand

Joined: Jun 03, 2012
Posts: 80

Thank you! I solved it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store the count value returned froma sql query into a variable for future use?