You are here:

Java/Result set is closed

Advertisement


Question
Hi Harper... Its nice to ask you doubts after long time..


import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

class fin1
{
 public static void main(String args[])
 {
   try
   {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection con = DriverManager.getConnection("jdbc:odbc:mvp");

     Statement st = con.createStatement();
     Statement st1 = con.createStatement();
     int i = 0;
  String op,str1,so,ss,pp,ph;
  StringBuffer stt,pf;
     ResultSet rs = st.executeQuery("select ipm from mvp ");
     ResultSet rs1 = st1.executeQuery("select ipp from mvp ");
     while (rs.next())
     {
   str1 = rs.getString(1);
       int k = str1.indexOf(';');
      op = str1.substring(0, k);
       System.out.println(str1);
       System.out.println(op);
       String qq="UPDATE mvp SET opm = '" + op + "' , opp = '" + op +"' WHERE ipm = '" + str1 + "'";
  System.out.println(qq);
  st.execute(qq);
    }
}
   catch (Exception localException)
   {
     System.out.println(localException);
   }
 }
}



when ever i execute this query it shows resultset is closed error.

how to fix it.

Thanks.

Answer
I think the issue is that you are trying to reuse the same Connection object for multiple queries. What you should do is use the update functionality of ResultSet, which will let you update the value of the current row as you are updating it. To do this you need to modify your query to include the 'opm' field like:
ResultSet rs = st.executeQuery("select ipm,opm from mvp ");

Then in your while loop:
rs.updateString(1,op);

This should allow you to update the value without having to execute another query.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.