• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrieve record from DB?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here in my code i have the operation of getting the Maximum primary key and i have to make use for numbering the persons registering.

QUERY


Here in this query it results as


person_id
5



Then how can i read the value of the person_id in the java program. As i had tried to make use of the getInt(),getString() methods with the result set where it specifies the output as

java.sql.SQLException: No current row in the ResultSet.



I am not sure how to solve this problem.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two options:
1) check if rs.next() (where rs is a ResultSet) returns true; if so, read the result, otherwise there are no rows and use 1

2) use "select MAX(person_id) AS MaxID from persons". That will always return a value, possibly NULL. If you use getInt, that NULL will be converted to 0.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, you didn't call rs.next() prior getting the result from the set.
 
Hari Dhanakoti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I got a temporary solution.

Where if i make use of the PreparedStatement then the problem arises that no result set found, but in case if i make use of the ordinary statement it has no problem with retrieving information from the database.

I had used only the rs.next() in while loop no other way the result set values are retrieved.

Can anyone say me what could be the reason for the difference in using these two statements in this situation.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wing Rider:
Where if i make use of the PreparedStatement then the problem arises that no result set found, but in case if i make use of the ordinary statement it has no problem with retrieving information from the database.



Where is the code?
 
Hari Dhanakoti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Working Code


Error Code
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are 2 different queries pal. Use the same query with PreparedStatement you will get the result fine.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic