Why JAVA. SQL. sqlexception: after end of result set appears

Source: Internet
Author: User

Recently, I have been working on a small company-defined system, with many gains and ideas. I have never been able to share my gains here. I personally think that coding must be very careful. A little small problem may cause you to be confused, unable to find a clue, and then go to the Internet to find a lot of information and see what is wrong. In fact, it is not. We also need to look at the system errors reported for us. Careful analysis is actually a very simple problem: self-reviewCodeNot careful enough, just like a problem that has plagued me over the past two days. I have always felt that my code is correct, but it is just the result I don't want,Java. SQL. sqlexception: after end of result set.

This problem has also been found online, because when you reference multiple result sets,Because the previous result set has been closed with the databaseAnd you reference it again. Of course, the result is null. I will hear it in a single statement. Let's take a look at this section.ProgramRight:

1 // Find the ID corresponding to the selected status
2 Sql1 =   " Select stateid from state where statemessage =' "
3 + Statebean. getstatemessage () +   " ' " ;
4 Resultset RS1 = Con.exe cutequery (sql1 );
5 Try {
6 While (Rs1.next ()){
7 Stateid = String. valueof (rs1.getint ( 1 ));
8 }
9 } Catch (Sqlexception e ){
10 E. printstacktrace ();
11 }
12 // Find the ID of the selected Administrator
13 Sql2 =   " Select adminid from Admin where adminrealname =' "
14 + Adminbean. getadminrealname () +   " ' " ;
15 Resultset rs2 = Con.exe cutequery (sql2 );
16 Try {
17 While (Rs2.next ()){
18   Adminid = String. valueof (rs1.getint ( 1 ));
19 }
20 } Catch (Sqlexception e ){
21 E. printstacktrace ();
22 }

I wonder if you have noticed the error of row 18th, adminid = string. valueof (Rs.Getint (1)); The result set of rs2 must be referenced, but the result set of RS1 that has been disabled previously is referenced. So it appearsJava. SQL. sqlexception: after end of result setThis is a very small detail, but if you are not careful, it may take a long time. Of course, it is also a good thing to make a mistake once. It is also a matter of accumulated experience and a deeper impression. I hope you will not see such a small problem next time.

Note:Executequery() The method is encapsulated by me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.