File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes problem with  ....while(rs.next()) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem with  ....while(rs.next())" Watch "problem with  ....while(rs.next())" New topic
Author

problem with ....while(rs.next())

Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
i'm trying to generate a reference but i cant seem to be getting it right
can somebody help me please
thanks

strSQL=("select max(complainantid) from complainant");
logger.info("SQL: "+strSQL);

try {
pstmt=cn.prepareStatement(strSQL);
rs=pstmt.executeQuery();
}
catch(Exception e)
{
logger.info("info :"+e);
}

try{

while(rs.next())
{
int Number=rs.getInt(1);
// int rowNum+=1;


%>
<script>alert("Your Reference Number is:<%=Number %>");</script>
<%

}
rs.close();

}catch(Exception e)
{
logger.info("After generating the sequence number:");
}
%>
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What is the behaviour you expect and what is the problem?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
Statement (strSQL) is supposed to return the maximun sequence number generated and i'm trying to print it out on the jsp... i think i'm messing up on the while.next... the max number is declared as an int.
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
What's the problem that you are facing? Are you getting exceptions? or the code executes but doesn't give any result

The only problem that I see is that if executeQuery generates an exception, rs will be null. You might want to move all your code inside a single try-catch block. That would save a lot of unnessescary operations and make your log much cleaner

Also, if you dont have any records in complainant, then nothing will come on the screen.

You can try this

Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
thanks for the response...
i've done just that but no luck...
the code execute fine but no number is generated.
when i run my SQL statement(strSQL) on SQL navigator it works fine cos i do get the max number but on the jsp i dont .
Padma Lalwani
Ranch Hand

Joined: Nov 02, 2004
Posts: 49
Maybe there is something wrong with the connection?
Padma
Kingsley Mullers
Ranch Hand

Joined: Jan 14, 2005
Posts: 48
thanks for the response once again, i think the connection is fine.
what i do is , i insert into a table using sequence(sequence.nextval) as primary key then i select the maximum number(current value)and use this as the reference number. i then try to generate this number on my jsp but the number is not printed.
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
Hi,

Is this problem solved? If not, can you furnish what info is logged? My instinct tells me there is an exception somewhere that you overlook.

Probably the ResultSet is closed when you try to access it. Did you close the PreparedStatement anywhere in your code. Check that out. If you close the PreparedStatement, the ResultSet automatically gets closed.


keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
 
Consider Paul's rocket mass heater.
 
subject: problem with ....while(rs.next())