A stmt multiple RS operation caused by ResultSet has been turned off error

Source: Internet
Author: User
Tags error code execution sql stmt
Error one stmt multiple RS for operation. Then the rs1 obtained from stmt, must immediately operate this rs1, to get another rs2, and then to the RS2 operation. cannot be used interchangeably with each other, which causes RS to be turned off incorrectly. The error code is as follows:  stmt= Conn.createstatement ();  rs=stmt.executequery ("SELECT * from T1"),  rst=stmt.executequery ("select * from T2") );  rs.last ()//due to the execution of Rst=stmt.executequery (sql_a), RS will be shut down! So the program execution prompts ResultSet to be closed. The error message is: Java.sql.SQLException:Operation not allowed after ResultSet closed rst.last (); Correct code:  stmt=conn.createstatement ();  rs=stmt.executequery ("SELECT * from T1");  rs.last ();// The operation of Rs should be done immediately, after the operation of the database to get RST, and then to RST operation  rst=stmt.executequery ("SELECT * from T2");  rst.last () The reason:  the   object  used  for  executing  a  static  sql  statement  and  returning  the  results  it  produces.     By  default,  only  one  resultset  object  per  statement  object  can  be  open  at  the   same  time.  Therefore,  if  the  reading  of  one  resultset  object  is  interleaved& nbsp with  the  reading  of  another,  each  must  have  been  generated  by  different  statement  objects.  all  execution  methods  in  the  statement  interface  implicitly  close  a  statment ' s  current  ' ResultSet  object  if  an  open  one  exists.      a stmt best corresponds to an RS, This can happen if you use a stmt to open two RS at the same time. So solve such problems: 1. To create a few stmt, a stmt corresponding to a rs;2. If you use a stmt corresponding to multiple RS, then you can only get one RS and then operate, Process the first RS and then process the other, such as the "correct code". Multiple stmt correspond to respective rs.stmt=conn.createstatement (); Stmt2=conn.createstatement (); rs= Stmt.executequery ("SELECT * from T1"), Rst=stmt2.executequery ("select * from T2"); Rs.last (); Rst.last ();

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.