aspose file tools*
The moose likes JDBC and the fly likes Retrieve record from DB? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Retrieve record from DB?" Watch "Retrieve record from DB?" New topic
Author

Retrieve record from DB?

Hari Dhanakoti
Ranch Hand

Joined: Jan 07, 2008
Posts: 74
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.


Regards,
Hari
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
I think, you didn't call rs.next() prior getting the result from the set.
Hari Dhanakoti
Ranch Hand

Joined: Jan 07, 2008
Posts: 74
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Jan 07, 2008
Posts: 74
Working Code


Error Code
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
These are 2 different queries pal. Use the same query with PreparedStatement you will get the result fine.
 
Don't get me started about those stupid light bulbs.
 
subject: Retrieve record from DB?