This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC and the fly likes Select single value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Select single value" Watch "Select single value" New topic
Author

Select single value

Bradly Fackrell
Greenhorn

Joined: Apr 05, 2011
Posts: 13
Is there a way to select a single item rather than a set?



All I want it the max ID. Reading the java.spl package all I see is the option to select a 'set' of data.
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 198
Hi Bradly,
Have you tried to print what there is in ResultSet?
have you tried something like that if you are writing JDBC code in a JSP-


A ResultSet points to its current row of data. Initially the cursor is positioned before the first row. The next method moves the pointer to the next row, and because it returns false when there are no more rows in the ResultSet object.
Bradly Fackrell
Greenhorn

Joined: Apr 05, 2011
Posts: 13
Thanks Ankit. I think that your example is the way to do it. My problem is that right now I can only read. I can't test my code becuse my school's Tomcat server is down.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

If you know for sure that there will be at most one row in your result set you can replace the "while" with an "if":
If you know for sure there will always be exactly one result, like in this case, you can even skip the if and just call rs.next() once:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 198
and welcome to JavaRanch,Bradly..........
Bradly Fackrell
Greenhorn

Joined: Apr 05, 2011
Posts: 13
Thanks Ankit. And thanks to you and Rob for your input.

Right now I'm running with Rob's second suggestions and everthing is working perfect.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Select single value