• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieve record from DB?

 
Hari Dhanakoti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Working Code


Error Code
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are 2 different queries pal. Use the same query with PreparedStatement you will get the result fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic