• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Yuta Lolap
Ranch Hand
Posts: 86
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I solved it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic